bash echo - how to denote the end of command

1.1k Views Asked by At
NL=$'\n'
CMD=""
CMD="$CMD echo Hello ; $NL"
CMD="$CMD echo World ; $NL"
$CMD

The above code gives the following output, the echo globing everything after it.

Hello ; echo World ;

Neither the new line character nor the semicolon does work here. What is going wrong?

1

There are 1 best solutions below

1
On

For enabling interpretation of backslash escapes you have to add -e flag to echo.

:/$ NL='\n'

Putting line feed into NL variable.

:/$ CMD=""

Setting CMD to empty string.

:/$ CMD="$CMD echo Hello ; $NL"

Resetting CMD, since old CMD is "" then new is " echo Hello ; \n"

:/$ CMD="$CMD echo World ; $NL"

Resetting CMD, since old CMD is " echo Hello ; \n" then new is " echo Hello ; \n echo World ; \n"

:/$ echo -e $CMD
echo Hello ; 
 echo World ;