cynkiller cynkiller - 11 days ago 10
Bash Question

Why is the file output to variable append unrelated file names under the path

I need to print the last line of file e.g. /path/file to an argument

Content:

>tail -n 1 /path/file
74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 **


The output from command line is correct.

But when I use:

arg=`tail -n 1 /path/file`
or arg=$(tail -n 1 /path/file)
echo $arg


The output become

74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 file1 file2 file3 ....
file1 file2 file3 are under /path


Seems the ** is translated to something like
ls
?
I don't quite understand the meaning...

How could I get the right $arg here?

Thanks!

PS. PS.
Answer

** is getting expanded ,which is resulting in listing all the files in the directory. This is called Globbing.

When not double or single quoted:

echo 74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 **
74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 file1 file2.....
more files..... and more files......

When double quoted:

echo "74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 **"
74.870 534.1 2187.7 0.000 60.0 32047 5.782 716.573 **

or set -f before doing echo to prevent wild card expansion.