Skip to content
Advertisement

Setting environment variable to command in a script

I made a bash script. It reads a file contains a list of commands and runs each of them.

A list of commands is like below.

JavaScript

Each line has a environment variable before command to set same name but different value to commands.

A script is like below.

JavaScript

I expect:

  1. run command1 with ENV_NUMBER=1
  2. run command2 with ENV_NUMBER=2
  3. run command3 with ENV_NUMBER=3

But, I ran script then I got error:

JavaScript

How do I fix it?

Advertisement

Answer

Your logic looks good from inside the command file…
See https://www.gnu.org/software/bash/manual/bash.html#Simple-Command-Expansion

The problem is that you are loading the variable assignment into a variable, rendering it as data rather than a command, then trying to context-hack it back.

JavaScript

is not the same as

JavaScript

which is what you are effectively doing.

Try this instead.

JavaScript

then either

JavaScript

or

JavaScript

This makes comandlist.txt the script, complete with the ampersands, which it otherwise kind of already was. Your program reads it in as data and then tries to convert it back to script a line at a time. Don’t do that.

c.f. https://mywiki.wooledge.org/BashFAQ/050

Original answer – for reference

Are you sure you are using bash?
Do you have something like #!/bin/bash as the first line of your script?

Here’s my simple example script:

JavaScript

Running it with no x value:

JavaScript

Running it with an x set:

JavaScript

Explicitly setting it temporarily in the command itself:

JavaScript

Running again without setting to show it’s temporary…

JavaScript

Explicitly exporting in the parent environment:

JavaScript

Manually overriding the exported value by supplying a temporary value from the command line:

JavaScript

Running again with no special edit, using the still-exported value:

JavaScript

So obviously what you are doing works fine in bash.
As I mentioned above, maybe you are not using bash.
Make sure you have a shebang. #! have to be the very first two characters in the file for it to work – no spaces above or before, no comments, nothing.

Let us know if that doesn’t help.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement