-1

I have a list of variables I copy-paste one by one from a file to my shell and hit Enter to execute. For example:

var1="myvar1"
var2="myvar2"

Because in practice there are more variables, instead copy-pasting them and execute them one by one I desire to do all in one operation to save time.
I tried copy-pasting:

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

echo $var1 outputs an empty line instead date in relevant format.

How could I execute a few variables in one operation?

0
3

You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.

The reason that

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

doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final ):

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

It's explained in Commpound Commands section in 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.

Later on it also says:

{ 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.

So that would work as commands would not be executed in a subshell but in a current shell environment:

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

I see you're asking a lot of simple questions about Bash. Consider reading some Bash tutorials, man bash and learn about common Bash pitfalls.

1
  • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me... – user149572 Aug 22 '19 at 11:16
1

The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.

I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:

var1="myvar1" var2="myvar2"

Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy