Вставить новую строку в начало команды, если она слишком длинная

Вам не нужно копировать их одну за другой, вы можете вставить все строки вместе, и новые строки будут работать как Enter.

Причина, по которой

(
var1="myvar1"
var2="myvar2"
)

не работает, потому что он выполняется в подоболочке. Это сработает, если вы напечатаете содержимое переменной перед окончательным):

(
var1="myvar1"
var2="myvar2"
echo $var2
)

Это объясняется в разделе Commpound Commandsв man bash:

.

Compound Commands

A compound command is one of the following. In most cases a list in a command's description may be sepa- rated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.

(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.

Далее также говорится:

{ list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter.

Так что это будет работать, так как команды будут выполняться не в подоболочке, а в текущей среде оболочки:

{
var1="myvar1"
var2="myvar2";
}

Я вижу, вы задаете много простых вопросов о Bash. Рассмотреть возможность прочтите несколько руководств по Bash, man bashи узнайте об общих Bash подводные камни .

1
30.09.2021, 14:39
0 ответов

Теги

Похожие вопросы