0

I am having trouble executing a command that has variables, so I am trying to recreate it here with this simple example.

I want to list the contents of the path DIR but because there is a space in the path it will not work. The problem is the space in the path /home/User Name/tarfolder. This works fine if there is no space in the path.

how can I get this to work for paths that have a space in the path?

Also on an aside is it not good practice to have spaces in paths with linux/unix. I am working on cygwin on a windows 7 machine, but i am writing my script for a linux server that I use.

SCRIPT:

#!/bin/bash

## trying to work on directories here that have spaces in there path 

# get the current directory the script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"  
echo "DIR is equal to:"
echo $DIR

## want to be able to list the contents of the path $DIR that has a space in it
eval 'ls' $DIR   

OUTPUT::

User Name@WNZCL0276 ~/tarfolder
$ ./dir_path_with_space.sh
DIR is equal to:
/home/User Name/tarfolder
ls: cannot access Name/tarfolder: No such file or directory
/home/User

I understand i can use escape characters like here, but I don't I can use that in my script above.

User Name@WNZCL0276 ~/tarfolder
$ ls /home/User\ Name/tarfolder/
backup.sh  dir_path_with_space.sh  folderToZip  ReadMe.txt
3
  • If you really needed to use eval, you could run eval 'ls "$DIR"', but... why? Jan 27, 2015 at 23:39
  • ...the outer quotes on right-hand side of the assignment don't hurt anything, by the way, but they also aren't necessary: Assignments implicitly prevent string-splitting and glob expansion. Jan 27, 2015 at 23:41
  • Similarly, echo "$DIR" is more correct than echo $DIR; if your directory is named /path/to/ *** my special directory name *** /, the latter will behave catastrophically (replacing the *s with lists of files in the directory where the current command is being run). Jan 27, 2015 at 23:44

1 Answer 1

3

Don't use eval. Do use quotes.

dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ls "$dir"

By convention, dir should be lower-case since it's neither an environment variable nor a shell builtin; following this convention avoids namespace collisions.


For a larger discussion of what you're trying to do here (in terms of finding your script's location), see BashFAQ #28. For a larger discussion of building commands using variables (with contents that can contain spaces and otherwise arbitrary content), see BashFAQ #50. For discussion of why eval shouldn't be used except when absolutely necessary, see BashFAQ #48.

7
  • tks for all that, I might be lacking in good practice when it comes to bash scripting. Also might have been a simple example for what I want to do. what I want to do is something like command="tar -cvf $arg1 $arg2" and the execute the command eval $command
    – HattrickNZ
    Jan 27, 2015 at 23:51
  • 1
    @HattrickNZ, read BashFAQ #50, which describes the correct way to do that (not involving eval). Jan 28, 2015 at 0:01
  • @HattrickNZ, in case you really want to keep a command in a shell variable and have trouble because parts of the command contain spaces, you should consider holding your command in a bash-array; eval can handle (i. e. execute) that as well and you can easily have spaces in parts of the command.
    – Alfe
    Jan 28, 2015 at 0:03
  • @Alfe, a command with its exact argv stored in an array can simply be run as "${array_name[@]}"; using eval throws away many of the benefits from using an array in the first place. Jan 28, 2015 at 0:05
  • I'm sorry if I was unclear about my intentions to use eval only if the things it provides are needed—like an additional evaluation-step of variables. I just meant that an array would not even rule out the eval in case it was necessary.
    – Alfe
    Jan 28, 2015 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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