Home > Mobile >  Simple question about ls output and Double quotes
Simple question about ls output and Double quotes

Time:10-16

I'm learning bash so this is a simple question probably. I'd like to understand what happens in this case, there's no real use for this script. Let DTEST be a directory that contains some random files.

for filename in " `ls DTEST/*` " ; do
        touch "$filename".txt
done

So the command substitution happens and the ls output should be file1 file2 file3. Then because the command substitution is double quoted the first line of the for loop should be for filename in "file1 file2 file3". I think it should create only one file named file1 file2 file3.txt.

But I've seen it wants to create a file named something like file1'$'\n''file2'$'\n''file3.txt.

I don't understand where the '$'\n'' come from. I read in bash manual that with double quotes \ followed by special characters like n for newline retains its special meaning, but why \n are generated?

CodePudding user response:

ls DTEST/* outputs each file on a separate line. Also, the output would contain the directory name (i.e. DEST/file1 etc.).

Of course you would never use ls in this way in a real script. Instead you would just use something like

for filename in DEST/*
do
 ...
done 
  • Related