1

I "inherited" some code in a project, and in one of the Bash scripts, they use echo on a backticks-row, like:

#!/bin/bash
echo `/path/command argument`

What's the difference between that and just running the command itself?

#!/bin/bash
/path/command argument

Both send the command's output to the script's stdout.

So what's the difference? Why use echo?

To combine it with a > seems even worse:

#!/bin/bash
echo `/path/command argument > /var/log/exemple.log`
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Puggan Se
  • 5,738
  • 2
  • 22
  • 48

1 Answers1

2

It doesn't look particularly useful in its current form but one side-effect of using echo with (unquoted) backticks is that whitespace between words is lost. This is because of word splitting - every word in the output of the command is treated as a separate argument to echo, which just outputs them separated by a single space. For example:

$ echo "a   b c"
a   b c
$ echo `echo "a   b c"`
a b c

Note that this applies to all types of whitespace such as tab characters and newlines.

I'm not sure why you'd want to do this deliberately! Personally, I'd be tempted to just run the command normally.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • 1
    It also changes newlines to spaces. – Keith Thompson Mar 16 '16 at 15:24
  • Yes, can be something about whitespace, putting `echo "a" | grep "b` in echo-bacticks makes an empty row, insted of zero rows, and putting `echo -e "hi\nhi"` in echo-bacticks makes one row instead of two – Puggan Se Mar 16 '16 at 15:24
  • 2
    The [useless use of `echo` in backticks](http://www.iki.fi/era/unix/award.html#echo) is a common, well-established antipattern. If a programmer is doing it on purpose, I would expect a comment to explain how this is not the common antipattern. – tripleee Mar 16 '16 at 15:27