Suppose I have the following variable in a bash script cmd='echo hi | wc'
.
Since I forgot how many characters are in the string “hi”, I want to run cmd
.
I expected this be as simple as typing $cmd
at a prompt and letting the shell’s infamous implicit word splitting take care of the rest.
But it seems that the one time I actually WANT word splitting, bash tries to protect me and the output is simply hi | wc
.
I stared at this in utter confusion for a bit, and eventually decided to try set -x
.
Turns out that bash is running echo hi '|' wc
.
That is, bash implicitly added quoting for me!
On one hand, this is usually great.
The shell scripts of the world would be in worse shape without this feature.
But that still leaves me with the unsolved problem of how to actually run the command echo hi | wc
.