Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am confused about what error code the command will return when executing a variable assignment plainly and with command substitution:

a=$(false); echo $?

It outputs 1, which let me think that variable assignment doesn't sweep or produce new error code upon the last one. But when I tried this:

false; a=""; echo $?

It outputs 0, obviously this is what a="" returns and it override 1 returned by false.

I want to know why this happens, is there any particularity in variable assignment that differs from other normal commands? Or just be cause a=$(false) is considered to be a single command and only command substitution part make sense?

-- UPDATE --

Thanks everyone, from the answers and comments I got the point "When you assign a variable using command substitution, the exit status is the status of the command." (by @Barmar), this explanation is excellently clear and easy to understand, but speak doesn't precise enough for programmers, I want to see the reference of this point from authorities such as TLDP or GNU man page, please help me find it out, thanks again!

share|improve this question
up vote 37 down vote accepted

Upon executing a command as $(command) allows the output of the command to replace itself.

When you say:

a=$(false)             # false fails; the output of false is stored in the variable a

the output produced by the command false is stored in the variable a. Moreover, the exit code is the same as produced by the command. help false would tell:

false: false
    Return an unsuccessful result.

    Exit Status:
    Always fails.

On the other hand, saying:

$ false                # Exit code: 1
$ a=""                 # Exit code: 0
$ echo $?              # Prints 0

causes the exit code for the assignment to a to be returned which is 0.


EDIT:

Quoting from the manual:

If one of the expansions contained a command substitution, the exit status of the command is the exit status of the last command substitution performed.

Quoting from BASHFAQ/002:

How can I store the return value and/or output of a command in a variable?

...

output=$(command)

status=$?

The assignment to output has no effect on command's exit status, which is still in $?.

share|improve this answer
    
Very good answer :)! Can you please help me find out the reference of the exit code is the same as produced by the command, I spend a lot of time but did not get the result :( – Reorx Nov 23 '13 at 3:25
    
@Reorx Added a couple of references in the edit above. – devnull Nov 23 '13 at 3:31
    
Thanks, that's clear enough :D – Reorx Nov 23 '13 at 3:39
7  
Keep in mind that this doesn't apply to local variables (eg, local output=$(command)), because local is a command itself and its exit-code will overwrite that of the assigned function. – sevko Jul 19 '14 at 2:45
3  
Regarding @sevko & @qodeninja 's comments about locals, this can easily be circumvented by initialization after declaration unless the variable is also readonly: local my_var; my_var=$(command); local status=$? – Adrian Günter Jul 26 '15 at 1:10

You are using the last return variable

$?

gives the return value of the last function or program executed. Executing false will end in 0, but assigning something to false does not return, so the 1 you are getting is actually the return code of the program PREVIOUSLY executed.

share|improve this answer
    
No, a=$(false) does return 1 because of the false. – Kevin Nov 23 '13 at 3:00
3  
When you assign a variable using command substitution, the exit status is the status of the command. – Barmar Nov 23 '13 at 3:10

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.