The following works in my shell (zsh):
> FOO='ls'
> $FOO
file1 file2
but the following doesn't:
> FOO='emacs -nw'
> $FOO
zsh: command not found: emacs -nw
even though invoking emacs -nw directly opens Emacs perfectly well.
Why?
The following works in my shell (zsh):
> FOO='ls'
> $FOO
file1 file2
but the following doesn't:
> FOO='emacs -nw'
> $FOO
zsh: command not found: emacs -nw
even though invoking emacs -nw directly opens Emacs perfectly well.
Why?
Because there's no command called emacs -nw. There's a command called emacs to which you can pass a -nw option.
To store commands, you generally use functions:
foo() emacs -nw "$@"
foo ...
To store several arguments, you generally use arrays:
foo=(emacs -nw)
$foo ...
To store a string containing several words separated by spaces and have it split on spaces, you can do:
foo='emacs -nw'
${(s: :)foo} ...
You could rely on word splitting performed on IFS (IFS contains space, tab, newline and nul by default):
foo='emacs -nw'
$=foo ...
For future readers it should be mentioned that the "standard" way of doing such thing is to evaluate the arguments stored in variable:
eval "$foo"
One can also evaluate expression with command substitution:
$(expr "$foo")
echoed variable (or otherwise printed) works as well:
$(echo "$foo")
These methods works fine at least in zsh and bash.