When you write ./dumpargs.sh '1 space' '2 space'
on the command line, the shell interprets the single-quotes before passing arguments to the script. Argument #1 will have the value 1 space
, argument #2 will ahve the value 2 space
. The single-quotes are not part of the values.
When you write ./dumpargs.sh $(cat testfile.txt)
,
the shell will not try to interpret the content of testfile.txt
.
The shell only interprets the actual text entered on the command line.
Substituted content like this example, or for example values of variables are not interpreted, but used literally.
Finally, the shell performs word splitting on the string on the command line, using the delimiters in IFS
.
So the single-quotes will be used literally,
and the content is simply split by whitespace.
One possible workaround is to store one argument per line, without single-quotes, and make dumpargs.sh
take the arguments from standard output:
while read -r line; do
echo "$line"
done
You can call it with ./dumpargs.sh < testfile.txt
.