What is wrong with “echo $(stuff)” or “echo `stuff`”?

31

6

I used one of the following

echo $(stuff)
echo `stuff`

(where stuff is e.g. pwd or date or something more complicated).

Then I was told this syntax is wrong, a bad practice, non-elegant, excessive, redundant, overly complicated, a cargo cult programming, noobish, naive etc.

But the command does work, so what exactly is wrong with it?

Kamil Maciorowski

Posted 2018-08-27T21:47:28.647

Reputation: 38 429

11

It's redundant and therefore should not be used. Compare with Useless Use Of Cat: http://porkmail.org/era/unix/award.html

– dr01 – 2018-08-28T08:20:11.427

7echo $(echo $(echo $(echo$(echo $(stuff))))) also does work, and still you probably wouldn't use it, right? ;-) – Peter - Reinstate Monica – 2018-08-29T07:57:26.553

1What are you trying to do with that expansion, exactly? – curiousguy – 2018-08-29T08:08:25.083

@curiousguy I'm trying to help other users, hence my answer below. Recently, I've seen at least three questions that use this syntax. Their authors were trying to do… various stuff. :D – Kamil Maciorowski – 2018-08-29T08:15:11.697

2Also, don't we all agree that it's unwise to confine oneself to an echo chamber?? ;-) – Peter - Reinstate Monica – 2018-08-29T08:24:06.810

Does the tag [tag:sh] imply a strictly /bin/sh question? – curiousguy – 2018-08-29T08:26:10.797

@curiousguy "The standard command language interpreter". I added this to indicate shells compatible and/or derived from sh (e.g. not [tag:Powershell]). It's not about any single shell; it's about the concept of using echo with command substitution this way. – Kamil Maciorowski – 2018-08-29T08:38:49.517

@KamilMaciorowski There are mostly compatible shells with support for all basic constructs but with sometimes different semantics. zsh is interesting. – curiousguy – 2018-08-29T09:12:25.753

Answers

62

tl;dr

A sole stuff would most probably work for you.



Full answer

What happens

When you run foo $(stuff), this is what happens:

  1. stuff runs;
  2. its output (stdout), instead of being printed, replaces $(stuff) in the invocation of foo;
  3. then foo runs, its command line arguments obviously depend on what stuff returned.

This $(…) mechanism is called "command substitution". In your case the main command is echo which basically prints its command line arguments to stdout. So whatever stuff tries to print to stdout is captured, passed to echo and printed to stdout by echo.

If you want the output of stuff to be printed to stdout, just run the sole stuff.

The `…` syntax serves the same purpose as $(…) (under the same name: "command substitution"), there are few differences though, so you cannot blindly interchange them. See this FAQ and this question.


Should I avoid echo $(stuff) no matter what?

There is a reason you may want to use echo $(stuff) if you know what you're doing. For the same reason you should avoid echo $(stuff) if you don't really know what you're doing.

The point is stuff and echo $(stuff) are not exactly equivalent. The latter means calling split+glob operator on the output of stuff with the default value of $IFS. Double quoting the command substitution prevents this. Single quoting the command substitution makes it no longer be a command substitution.

To observe this when it comes to splitting run these commands:

echo "a       b"
echo $(echo "a       b")
echo "$(echo "a       b")"  # the shell is smart enough to identify the inner and outer quotes
echo '$(echo "a       b")'

And for globbing:

echo "/*"
echo $(echo "/*")
echo "$(echo "/*")"  # the shell is smart enough to identify the inner and outer quotes
echo '$(echo "/*")'

As you can see echo "$(stuff)" is equivalent(-ish*) to stuff. You could use it but what's the point of complicating things this way?

On the other hand if you want the output of stuff to undergo splitting+globbing then you may find echo $(stuff) useful. It has to be your conscious decision though.

There are commands generating output that should be evaluated (which includes splitting, globbing and more) and run by the shell, so eval "$(stuff)" is a possibility (see this answer). I have never seen a command that needs its output to undergo additional splitting+globbing before being printed. Deliberately using echo $(stuff) seems very uncommon.


What about var=$(stuff); echo "$var"?

Good point. This snippet:

var=$(stuff)
echo "$var"

should be equivalent to echo "$(stuff)" equivalent(-ish*) to stuff. If it's the whole code, just run stuff instead.

If, however, you need to use the output of stuff more than once then this approach

var=$(stuff)
foo "$var"
bar "$var"

is usually better than

foo "$(stuff)"
bar "$(stuff)"

Even if foo is echo and you get echo "$var" in your code, it may be better to keep it this way. Things to consider:

  1. With var=$(stuff) stuff runs once; even if the command is fast, avoiding computing the same output twice is the right thing. Or maybe stuff has effects other than writing to stdout (e.g. creating a temporary file, starting a service, starting a virtual machine, notifying a remote server), so you don't want to run it multiple times.
  2. If stuff generates time-depending or somewhat random output, you may get inconsistent results from foo "$(stuff)" and bar "$(stuff)". After var=$(stuff) the value of $var is fixed and you can be sure foo "$var" and bar "$var" get identical command line argument.

In some cases instead of foo "$var" you may want (need) to use foo $var, especially if stuff generates multiple arguments for foo (an array variable may be better if your shell supports it). Again, know what you're doing. When it comes to echo the difference between echo $var and echo "$var" is the same as between echo $(stuff) and echo "$(stuff)".


*Equivalent(-ish)?

I said echo "$(stuff)" is equivalent(-ish) to stuff. There are at least two issues that make it not exactly equivalent:

  1. $(stuff) runs stuff in a subshell, so it's better to say echo "$(stuff)" is equivalent(-ish) to (stuff). Commands that affect the shell they run in, if in a subshell, don't affect the main shell.

    In this example stuff is a=1; echo "$a":

    a=0
    echo "$(a=1; echo "$a")"      # echo "$(stuff)"
    echo "$a"
    

    Compare it with

    a=0
    a=1; echo "$a"                # stuff
    echo "$a"
    

    and with

    a=0
    (a=1; echo "$a")              # (stuff)
    echo "$a"
    

    Another example, start with stuff being cd /; pwd:

    cd /bin
    echo "$(cd /; pwd)"           # echo "$(stuff)"
    pwd
    

    and test stuff and (stuff) versions.

  2. echo is not a good tool to display uncontrolled data. This echo "$var" we were talking about should have been printf '%s\n' "$var". But since the question mentions echo and since the most probable solution is not to use echo in the first place, I decided not to introduce printf up until now.

  3. stuff or (stuff) will interleave stdout and stderr output, while echo $(stuff) will print all the stderr output from stuff (which runs first), and only then the stdout output digested by echo (which runs last).

  4. $(…) strips off any trailing newline and then echo adds it back. So echo "$(printf %s 'a')" | xxd gives different output than printf %s 'a' | xxd.

  5. Some commands (ls for example) work differently depending if the standard output is a console or not; so ls | cat does not the same ls does. Similarly echo $(ls) will work differently than ls.

    Putting ls aside, in a general case if you have to force this other behavior then stuff | cat is better than echo $(ls) or echo "$(ls)" because it doesn't trigger all the other issues mentioned here.

  6. Possibly different exit status (mentioned for completeness of this wiki answer; for details see another answer that deserves credit).

Kamil Maciorowski

Posted 2018-08-27T21:47:28.647

Reputation: 38 429

5One more difference: the stuff way will interleave stdout and stderr output, while echo `stuff` will print all the stderr output from stuff, and only then the stdout output. – Ruslan – 2018-08-28T05:55:01.153

3And this is another example for: There can hardly be too many quotes in bash scripts. echo $(stuff) can put you into far more troubles as echo "$(stuff)" if you do not want globbing and expansion explicitly. – rexkogitans – 2018-08-28T07:01:01.973

2One more slight difference: $(…) strips off any trailing newline and then echo adds it back. So echo "$(printf %s 'a')" | xxd gives different output than printf %s 'a' | xxd. – derobert – 2018-08-28T19:39:42.770

1You should not forget that some commands (ls for example) work differently depending if the standard output is a console or not; so ls | cat does not the same ls does. And of course echo $(ls) will work differently than ls. – Martin Rosenau – 2018-08-29T05:14:02.100

@Ruslan The answer is now community wiki. I have added your useful comment to the wiki. Thank you. – Kamil Maciorowski – 2018-08-29T09:42:00.577

@derobert The answer is now community wiki. I have added your useful comment to the wiki. Thank you. – Kamil Maciorowski – 2018-08-29T09:42:37.547

@MartinRosenau The answer is now community wiki. I have added your useful comment to the wiki. Thank you. – Kamil Maciorowski – 2018-08-29T09:43:01.483

5

Another difference: The sub-shell exit code is lost, so the exit code of echo is retrieved instead.

> stuff() { return 1
}
> stuff; echo $?
1
> echo $(stuff); echo $?
0

WaffleSouffle

Posted 2018-08-27T21:47:28.647

Reputation: 151

3Welcome to Super User! Can you explain the difference you are demonstrating? Thanks – bertieb – 2018-08-29T09:04:02.107

4I believe that this shows that the return code$? will be the return code of echo (for echo $(stuff)) instead of the one returned by stuff. The stuff function return 1 (exit code), but echo sets it to "0" regardless of the return code of stuff. Still should be in the answer. – Ismael Miguel – 2018-08-29T09:32:16.507

the return value from the subshell has been lost – phuclv – 2018-08-29T09:50:43.317