1

Consider the following bash transcript:

~$ x="echo -n abc "

~$ $x && echo xyz abcxyz

~$

Out of curiosity (please indulge me), I want to have a bash variable containing the echo -n command outputting something. I want to know if it is possible to define it in a way to preserve the trailing space.

An obvious option is use the quotes:

~$ x="echo -n \"abc \""

~$ $x && echo xyz "abc "xyz

~$

But these quotes pollute the output.

Of course, I can prepend the space to the second echo command:

~$ x="echo -n abc "

~$ $x && echo " xyz" abc xyz

~$

But the purpose of the question is to understand if x can be changed in a way that $x represents echo -n with the trailing space without redundant quotes in the output.

Thank you very much.

mark
  • 387

1 Answers1

2

This is suffering from I'm trying to put a command in a variable, but the complex cases always fail!

The solution is to use an array:

$ x=(echo -n "abc ")
$ "${x[@]}" && echo xyz
abc xyz
glenn jackman
  • 85,964