$ ls -l /tmp/test/my\ dir/
total 0

I was wondering why the following ways to run the above command fail or succeed? Thanks.

$ abc='ls -l "/tmp/test/my dir"'

$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory

$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory

$ bash -c $abc
'my dir'

$ bash -c "$abc"
total 0

$ eval $abc
total 0

$ eval "$abc"
total 0

This has been discussed in a number of questions on unix.SE, I'll try to collect all issues I can come up with here. References at the end.

Why it fails

The reason you face those problems is word splitting and the fact that quotes expanded from variables don't take effect.

The cases presented in the question:

$ abc='ls -l "/tmp/test/my dir"'

Here, $abc is split, and ls gets the two arguments "/tmp/test/my and dir" (with the quotes present):

$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory

Here, the expansion is quoted, so it's kept as a single word. The shell tries to find a program called ls -l "/tmp/test/my dir", spaces and quotes included.

$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory

And here, only the first word or $abc is taken as the argument to -c, so Bash just runs ls in the current directory. The other words are arguments to bash, and are used to fill $0, $1, etc.

$ bash -c $abc
'my dir'

With bash -c "$abc", and eval "$abc", there's an additional shell processing step, which does make the quotes work, but also causes all shell expansions to be processed again, so there's a risk of accidentally running a command expansion from user-provided data, unless you're very careful about quoting.

Better ways to do it

The two better ways to store a command are a) use a function instead, b) use an array variable.

Using a function:

Simply declare a function with the command inside, and run the function as if it were a command. Expansions in commands within the function are only processed when the command runs, not when it's defined, and you don't need to quote the individual commands.

# define it
myls() {
    ls -l "/tmp/test/my dir"

# run it

Using an array:

Arrays allow creating multi-word variables where the individual words contain white space. Here, the individual words are stored as distinct array elements, and the "${array[@]}" expansion expands each element as separate shell words:

# define the array
mycmd=(ls -l "/tmp/test/my dir")

# run the command

The syntax is slightly horrible, but arrays also allow you to build the command line piece-by-piece. For example:

mycmd=(ls)               # initial command
if [ "$want_detail" = 1 ]; then
    mycmd+=(-l)          # optional flag
mycmd+=("$targetdir")    # the filename


or keep parts of the command line constant and use the array fill just a part of it, options or filenames:

options=(-x -v)
files=(file1 "file name with whitespace")

transmutate "${options[@]}" "${files[@]}" "$target"

The downside of arrays is that they're not a standard feature, so plain POSIX shells (like dash, the default /bin/sh in Debian/Ubuntu) don't support them. Bash, ksh and zsh do, however.

Using "$@"

In shells with no support for named arrays, one can still use "$@" to hold the arguments of a command.

The following are portable script bits that does the equivalent of the code in the above section in /bin/sh, using the $@ list of positional parameters. Setting "$@" is done with set, and the double quotes around "$@" are important (these cause the elements of the list to be individually quoted).

First, simply storing a command with arguments in "$@":

set -- ls -l "/tmp/test/my dir"

Conditionally setting parts of the command line options for a command:

set -- ls
if [ "$want_detail" = 1 ]; then
    set -- "$@" -l
set -- "$@" "$targetdir"


Only using "$@" for options and operands:

set -- -x -v
set -- "$@" file1 "file name with whitespace"
set -- "$@" /somedir

transmutate "$@"

Be careful with eval!

As eval introduces an additional level of quote and expansion processing, you need to be careful with user input. For example, this works as long as the user doesn't type in any single quotes:

read -r filename
cmd="ls -l '$filename'"
eval "$cmd";

But if they give the input '$(uname)'.txt, your script happily runs the command substitution.

A version with arrays is immune to that since the words are kept separate for the whole time, there's no quote or other processing for the contents of filename.

read -r filename
cmd=(ls -ld -- "$filename")


  • 2
    you can get around the eval quoting thing by doing cmd="ls -l $(printf "%q" "$filename")". not pretty, but if the user is dead set on using an eval, it helps. It's also very useful for sending the command though similar things, such as ssh foohost "ls -l $(printf "%q" "$filename")", or in the sprit of this question: ssh foohost "$cmd". – Patrick May 20 '18 at 19:39
  • Not directly related, but have you hard-coded the directory? In that case, you might want to look at alias. Something like: $ alias abc='ls -l "/tmp/test/my dir"' – Hopping Bunny May 23 '18 at 2:25

The safest way to run a (non-trivial) command is eval. Then you can write the command as you would do on the command line and it is executed exactly as if you had just entered it. But you have to quote everything.

Simple case:

abc='ls -l "/tmp/test/my dir"'
eval "$abc"

not so simple case:

# command: awk '! a[$0]++ { print "foo: " $0; }' inputfile
abc='awk '\''! a[$0]++ { print "foo: " $0; }'\'' inputfile'
eval "$abc"

The second quote sign break the command.

When I run:

abc="ls -l '/home/wattana/Desktop'"

It gave me an error.

But when I run

abc="ls -l /home/wattana/Desktop"

There is no error at all

There is no way to fix this at the time(for me) but you can avoid the error by not having space in directory name.

This answer said the eval command can be used to fix this but it doesn't work for me :(

  • 1
    Yeah, that works as long as there's no need for e.g. filenames with embedded spaces (or ones containing glob characters). – ilkkachu May 20 '18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.