#!/bin/bash
RESULT=$(ls)
echo -e "$RESULT" # prints the result of 'ls' with new lines
echo -e $RESULT # prints the result of 'ls' in one line
Why does the second approach print all in one line instead of a new line for each item ? Should’nt the -e option trigger the interpretation of the \n charachters ?
When you run this command:
Bash performs word splitting on
$RESULT; that is, it splits it up by whitespace, and passes the resulting tokens as separate arguments toecho. So you’re essentially running this:and
echohas no way of knowing that$RESULTcontained newlines.(The
-e, by the way, isn’t really relevant here.-edoesn’t affect the treatment of newline characters, only of an actual sequence like\n— a backslash followed by ann.)