Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an inline variable assignment and a regular one in Bash?

What is the difference between:

prompt$   TSAN_OPTIONS="suppressions=/somewhere/file" ./myprogram 

and

prompt$   TSAN_OPTIONS="suppressions=/somewhere/file" prompt$   ./myprogram 

The thread-sanitizer library gives the first case as how to get their library (used within myprogram) to read the file given in options. I read it, and assumed it was supposed to be two separate lines, so ran it as the second case.

The library doesn't use the file in the second case, where the environment variable and the program execution are on separate lines.

What's the difference?

Bonus question: How does the first case even run without error? Shouldn't there have to be a ; or && between them? The answer to this question likely answers my first...

like image 901
user1902689 Avatar asked May 12 '15 01:05

user1902689


People also ask

What is the difference between regular shell variables and environment variables?

The difference between environment variables and regular shell variables (6.8) is that a shell variable is local to a particular instance of the shell (such as a shell script), while environment variables are "inherited" by any program you start, including another shell (38.4).

What character is often used in Bash to reference a variable?

You don't have to use any special character before the variable name at the time of setting value in BASH like other programming languages. But you have to use '$' symbol before the variable name when you want to read data from the variable.

What are environment variables in Bash?

In Bash, environment variables define many different things : your default editor, your current username or the current timezone. Most importantly, environment variables can be used in Bash scripts in order to modify the behaviour of your scripts.


1 Answers

The format VAR=value command sets the variable VAR to have the value value in the environment of the command command. The spec section covering this is the Simple Commands. Specifically:

Otherwise, the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment except as a side-effect of the expansions performed in step 4.

The format VAR=value; command sets the shell variable VAR in the current shell and then runs command as a child process. The child process doesn't know anything about the variables set in the shell process.

The mechanism by which a process exports (hint hint) a variable to be seen by child processes is by setting them in its environment before running the child process. The shell built-in which does this is export. This is why you often see export VAR=value and VAR=value; export VAR.

The syntax you are discussing is a short-form for something akin to:

VAR=value export VAR command unset -v VAR 

only without using the current process environment at all.

like image 139
Etan Reisner Avatar answered Sep 22 '22 17:09

Etan Reisner