Luke
Luke

Reputation: 623

Bash script placing quotes around command

I have a Bash script that will get an IP to use as part of an SSH tunnel, but running this script the SSH tunnel fails. When using set -x I can see it places the arguments to the SSH command in single quotes and manually running this line results in the same error.

The Script:

ssh -N -L 9000:${ip_array[$2]}:443 ssh-server

The first argument is used elsewhere in the script for something else which is why the second is used here. ssh-server is an alias in my SSH config to the server i am tunneling through.

The output I get is:

ssh -N -L '9000:"172.0.0.1":443' ssh-server

Could this be because the script to fetch the IP returns strings to the array?

Upvotes: 0

Views: 80

Answers (3)

David Deprost
David Deprost

Reputation: 5110

Get rid of the quotes by piping it through the tr command:

ssh -N -L 9000:$( echo ${ip_array[$2]} | tr -d '"' ):443 ssh-server

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246807

Or just use shell parameter expansion to remove the quotes:

ssh -N -L 9000:${ip_array[$2]//"/}:443 ssh-server

That lone double quote may mess up your editor's syntax highlighting.

Upvotes: 1

nullPointer
nullPointer

Reputation: 4574

you can try removing the double-quotes first :

ip=$(echo "${ip_array[$2]}" | sed "s/\"//g")
ssh -N -L 9000:${ip}:443 ssh-server

Upvotes: 1

Related Questions