1

Below is the code of bash:

a=`echo hello`
echo $a

output is :

hello

But I think it should be:

hello
0

hel
  • 581
  • 10
  • 26
  • 2
    the exitcode does not displayed you can do `echo $?` to print exitcode of last command – igreenfield Jul 27 '16 at 03:30
  • Can anyone tell me how to make key works of code highlight on SO? – hel Jul 27 '16 at 03:30
  • Could you please retitle this? It looks like you are asking what `$?` means (which of course is already answered [here](http://stackoverflow.com/questions/36530696/linux-how-to-get-error-description-by-error-number/36566055#36566055) or [here](http://stackoverflow.com/questions/36530696/linux-how-to-get-error-description-by-error-number/36566055#36566055)) but this seems to be about an incorrect assumption about the result of command substitution. – tripleee Jul 27 '16 at 04:27

3 Answers3

6

You think wrong ;-)

Putting the command in backticks assigns the output (stdout) from the expression on the right to the variable on the left.

$? gives you the "output status" (or return code) of the command - aka the "0" you were expecting.

So:

a=`echo hello`

Runs the command "echo hello" but instead of echoing to stdout, it "echoes" to varaiable a. So a=whatever_the_command_would_have_written_to_stdout (in this case "hello") - nothing is actually written to stdout because it is "captured" by the ``s

John3136
  • 28,809
  • 4
  • 51
  • 69
  • So what's the meaning and effect of `echo $a` in my sample code? – hel Jul 27 '16 at 03:35
  • 3
    `a=\`echo hello\`` translates to set variable a to the standard output of running the command `echo hello` or as you've seen when you did it: `a=hello` – John3136 Jul 27 '16 at 03:37
  • Indeed, I don't understand. Does the value of `a` is NULL? If it equals `a=hello`, then it would outputs two hello. That is : hello
    hello
    – hel Jul 27 '16 at 03:40
  • Your question includes proof that `$a` ends up with the value "hello" - Why are you now thinking it is NULL? – John3136 Jul 27 '16 at 03:41
  • `echo hello` means excutes command echo hello? And then `echo $a`, it would output hello twice. – hel Jul 27 '16 at 03:42
  • 1
    I've explained it. You have seen the results. You seem to be trying to make it more complicated than it is... – John3136 Jul 27 '16 at 03:43
  • 1
    @hel Though you should properly quote the result; `echo "$hello"`. – tripleee Jul 27 '16 at 04:23
0

You mistakenly think that a=`echo hello`:

  • executes echo hello and prints its stdout output directly to the caller's stdout,
  • and then assigns the exit code (return value) of the echo command to variable $a.

Neither is true; instead:

  • echo hello's stdout output is captured in memory (without printing to the caller's stdout; that's how command substitutions work),
  • and that captured output is assigned to $a.

A command's exit code (a return value indicating success vs. failure) is never directly returned in POSIX-like shells such as Bash.
The only way to use an exit code is either:

  • explicitly, by accessing special variable $? immediately after the command ($? contains the most recent command's exit code)

  • implicitly, in conditionals (a command whose exit code is 0 evaluates to true in a conditional, any other exit code implies false).

Thus, to achieve what you're really trying to do, use:

echo 'hello'  # Execute a command directly (its stdout output goes to the caller's stdout)
a=$?          # Save the previous command's exit code in var. $a
echo "$a"     # Echo the saved exit code.
mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

As this [ this ] answer already mentioned, the return value for the last executed command is stored in

$? # ${?} is sometimes needed

If you wish a to contain 'hello' and the return value of echo hello in separate lines, ie

hello
0

below is one way to do it

$ a=`echo -en "hello\n" && echo -n $?` # here $? is ret val for 1st echo
$ echo -e "$a"
hello
0

Note

  • -n with echo suppresses the trailing new line
  • -e with echo interprets escape sequences
  • Since && is the logical and operator, the second echo wouldn't have been executed had the first echo failed
  • Another important point to note is that even the assignment ie

    a=b
    

    has a return value.

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • To the downvoter, I understood the first answer was crap. But I have rectified the issue with the edit. You may consider retracting the downvote. – sjsam Jul 27 '16 at 04:55
  • From the comments to [this](http://stackoverflow.com/a/38603247/6379160) answer, it's clear that the op doesn't follow simple explanation. Also this doesn't t answer the question. – Мона_Сах Jul 27 '16 at 05:06