One of my coworkers has provided me with a Bash syntax that I am unfamiliar with. My Google foo has failed me on figuring out what it does and why/when I should use it.

The command that he sent me was of this form:

someVariable=something command

Initially, I thought that this was equivalent to the following:

someVariable=something ; command

Or someVariable=something command

But this doesn't appear to the be case. Examples:

enter image description here

What does this syntax do? What happens to the variable that has been set? Why does this work?

share|improve this question
up vote 14 down vote accepted

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.

Note: bear in mind that this is not specific to bash, but specified by POSIX.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]
share|improve this answer
    
If that were true, then BAZ=jake echo $BAZ should print jake. – sixtyfootersdude 9 hours ago
3  
@sixtyfootersdude: No, it shouldn't, as variable expansion happens before the command is run. Cf. foo=bar eval echo '$foo' – choroba 9 hours ago
    
Right that makes sense. Do you mind if I explain why in your question? – sixtyfootersdude 9 hours ago
    
@sixtyfootersdude I you mean "in my answer", I don't mind, but Stephen Kitt's answer covers this quite well already – xhienne 9 hours ago
1  
@sixtyfootersdude Thanks for your edit. I have replaced the image with a similar text that can be copy-pasted (images with code are frowned upon here IIRC). Hope you won't mind. I have also added relevant excerpts of the bash manual. – xhienne 9 hours ago

These are variable assignments in the context of simple commands. As xhienne has mentioned, for external commands they are equivalent to exporting the assigned value(s) for the duration of the command.

In your examples you're using a built-in command, so the behaviour isn't quite the same: the assignments affect the current environment, but whether the effect lasts after the execution of the built-in command is unspecified. To understand your examples, you need to know that parameter expansion happens before variables are handled; thus with

 BAZ=jake echo $BAZ

the shell first expands $BAZ (resulting in nothing), then sets BAZ to jake, and finally runs

 echo

which prints an empty line. (Then the shell forgets BAZ as your subsequent echo $BAZ shows.)

 BAZ=jake; echo $BAZ

is interpreted as two commands: first the BAZ variable is set in the current environment, then echo $BAZ is expanded to echo jake and executed.

share|improve this answer
    
So if I understand this correctly, running func(){ echo $BAZ }; BAZ=jake func would output jake, but not set BAZ to jake in the running shell ? – Valentin B. 9 hours ago
    
@ValentinB. exactly; try it and you'll see! – Stephen Kitt 9 hours ago
    
Well, that's always handy to know, thanks and props to your detailed answer ! – Valentin B. 9 hours ago

There are key a few key things that happen here:

  • As explained in bash reference manual, Simple Command Expansion, section "If no command name results, the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment." Thus, when you say var="something" command arg1 arg2, the command will run with var being in command's environment and disappearing after command exits. Demonstration of this is simple - access the command's environment from within the command:

    $ BAZ="jake" python -c "import os; print os.environ['BAZ']"                                                              
    jake
    

    This is nothing to be surprised about. Such syntax is frequently used to run programs with modified environment. My fellow unix.stackexchange.com and askubuntu.com users will recognize this example: if user uses German locale, and you only speak in English, you can ask them to reproduce whatever issue they're having and obtain output in English like so:

    LC_ALL=C command
    

    Note also that environment access isn't specific to python. It can be done with C or any other programming language. It just so happened to be my "weapon of choice" right now and only used for this demo.

  • The variable expansion occurs before anything runs. Thus, when you run something like BAZ="foo" echo $BAZ, the shell looks into its environment first, finds no variable BAZ there , and thus leaves $BAZ empty. Simple demonstration of that is:

    $ BAZ="jake" python -c "import sys; print 'ARG:',sys.argv[1]" $BAZ                                                       
    ARG:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    IndexError: list index out of range
    

It's important to also note, the distinction from BAZ=jake; echo $BAZ which is two separate command statements and here BAZ=jake will stay in shell's environment. The use case depends on your intentions. If you intend running more than one program that needs such variable, it might be desirable to export such variable. If you only need it for this one specific time, then preceding variable assignments are might be preferable.

share|improve this answer

I 'think' you may be overthinking it. So we start with: someVariable=something command

The assignment to someVariable is whatever is returned from the right. Not knowing the actual items on the right makes this harder to diagnose, but are you sure on your assumption that it is "something command"? Are you sure it is not "command something"? Or even "command command"?

Try seeing if "something" and "command" actually point to something in the path:
which something
which command

Also like you echoed out $BAZ, see if you get something by echoing out $something and $command (you could also just look at the environment variables to see if something is there).

Without more precise details on the actual bash line, my guess is that "jake" is a command (or an alias, etc.) that takes the rest of the line as it's parameter.

share|improve this answer
    
This is not an answer because: (1) It's purely a guess; (2) It is confusingly written and doesn't clearly communicate what you are thinking; (3) The parts that are understandable are also wrong. – Wildcard 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.