3

I have this script:

port=$1
cmd=$2
ssh [email protected] -p $port "'"$cmd"'"

When running myscript.sh 1000 "ls /dev/shm" it get this error:

bash: ls /dev/shm: No such file or directory

In the script, if I echo the command (to see how it's evaluated) like this:

echo ssh [email protected] -p $port "'"$cmd"'"

I get this:

echo ssh [email protected] -p $port "'"$cmd"'"

Which is the proper command... So what is it that I am missing here?

Thank you.

1 Answer 1

4

Just found out how to fix the problem so here is the guide:

How to troubleshoot commands that wont execute properly from within a script

aka

How to view exactly what is being executed by the shell

In the script, enclose your problematic command with the set command:

set -x #unhide debug info
your command
set +x #hide debug info

In my previous example, were port=1000 and cmd="ls /dev/shm":

ssh [email protected] -p $port "'"$cmd"'"

Was actually sent to the shell and executed as:

ssh [email protected] -p 1000 ''\''ls' '/dev/shm'\'''

Look at that! Now we all know, double quotes " " are in fact an hyperactive bipolar single quote in disguise just waiting around the corner to mess with you... and sometimes it brings a friend called Mr. backslash.

The solution was to use double quotes in the script:

ssh [email protected] -p $port "$cmd"

When combined with the double quotes from the command line the quoting get translated and executed as single quotes ' ' like this:

ssh [email protected] -p 1000 'ls /dev/shm'    

Who knew! you did? good for you!

Just another way nix messes with us :P

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.