Pregunta

I just learned that, quoting make a huge difference in some cases, and I did something to test it, here is what I just did,

$ xfs=$(find . -type f -perm -111)  #find all files with x-perm
$ echo "$xfs"
./b.out 
./a.out
$ echo $xfs
./b.out ./a.out  #why all in one line, but the above takes two?

If $xfs contains \n, AFAIK, echo -e will expand \n, but how can echo "$xfs" take 2 lines?

¿Fue útil?

Solución

Any whitespace is normally in shell considered to be an argument separator. Thus, your first example has two arguments. echo prints the arguments separated by one space, and that's the behaviour you see in your second example.

However, when you use quotes, anything between them is one argument, and it is printed literally. The one argument in your first example already contains a newline, so it is printed with a newline.

The -e option from the bash echo builtin regulates the expansion of escape sequences like \n; however, you don't have any escape sequences. The variable contains a literal newline.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top