Gerardo Zinno Gerardo Zinno - 5 months ago 49
Bash Question

Bash script takes wrong conditional path for unknown reason

I have the following bash script that should list the folders in the same directory, and let me choose a folder to move in, and then list its content.



select dest in $( command ls -aF | grep "/" ); do
if [ -d $dest ]; then
cd $dest
echo "$0 : changed to $dest"

echo "$0 : wrong choice" 1>$2

the path of the script is something like


and this is the content of the Exercise4 dir

1/ 2/ 3/ 4/ 5/

when I run the script it prompts me something like

1) ./
2) ../
3) 1/
4) 2/
5) 3/
6) 4/
7) 5/

If I choose an option between 1 and 7 the script works, but when I input a number out of that range, instead of echoing me "wrong choice" it lists me my home directory and I can't figure out why

Answer Source

Why This Happens


[ -d $dest ]

What does this do? It runs the command:

[ -d ]

What does that do? It's shorthand for:

[ -n "-d" ]

...which is to say, it checks whether -d is empty, which it isn't, so the result is true.

How To Stop It

  1. Use More Quotes

    Consider this instead:

    [ -d "$dest" ]

    When run, it doesn't invoke [ -d ]; instead, it runs [ -d '' ]: The quotes prevented the expansion results from being split into a different number of strings than they started as.

  2. When On Bash, Consider The Extended Test Form

    [[ -d $dest ]]

    ...suppresses string-splitting and glob expansion, so it works even without quotes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download