The current directory contains file1 and file2. How do you explain this output:
[root@server test]# command='*';
[root@server test]# echo $command
file1 file2
Why is * expanded? I expect it not to be because it's quoted.
The current directory contains file1 and file2. How do you explain this output:
[root@server test]# command='*';
[root@server test]# echo $command
file1 file2
Why is * expanded? I expect it not to be because it's quoted.
command='*' does indeed as you expect set the value of $command to a literal *. However, when you run echo $command, the value of $command is expanded to *, making your command echo *. The wildcard is then expanded as per usual. To prevent this, quote the parameter you are giving to echo:
$ v='*'
$ echo "$v"
*
$commandis unquoted. Try"$command", and you'll get*. – Benjamin W. Nov 15 '18 at 15:08$commandinto*), and then filename expansion (turns*into filenames in the current directory). Quoting suppresses that second expansion. – Benjamin W. Nov 15 '18 at 15:13