Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad stdio forwarding specification '%h:%p''

Tags:

bash

shell

macos

[OS - macOS, Shell - Bash]

I have a small bash script that generates a ssh command based on input parameters. The command generated is:

ssh -o ProxyCommand='ssh <bastion_name> -W %h:%p' -A -D <port> <username>@<destination_host_name>

The script stores this command in a variable say cmd, where

cmd="ssh -o ProxyCommand='ssh $1 -W %h:%p' -A -D $3 $4@$2"

where $1 = bastion_name, $2 = destination_host_name, $3 = port and $4 = username.

and then it tries to execute the command like,

echo "Executing --> $cmd"
$cmd

but it terminates with following error,

Bad stdio forwarding specification '%h:%p''

But since I am echoing the cmd, I copy paste directly in terminal and it runs without any error.

Please note the bastion_name is resolved via ssh config which has the username and other attributes defined for that bastion host.

What's the problem here because obviously the command is correct ?

like image 298
Vijayant Avatar asked Feb 25 '26 06:02

Vijayant


2 Answers

Try not to use eval, cause it's evil.

Always quote your variables (unless you have a reason not to). Never $cmd always "$cmd".

Use bash arrays:

cmd=(ssh -o ProxyCommand='ssh '"$1"' -W %h:%p' -A -D "$3" "$4@$2")

echo "Executing -->" "${cmd[@]}"
"${cmd[@]}"

Learn more about quoting here.

@edit: Took the $1 part out of single quotes, cause OPs intent is to expand it. Fixed executing --> line.

like image 182
KamilCuk Avatar answered Feb 27 '26 03:02

KamilCuk


This works,

cmd=("-o ProxyCommand='ssh $bastion -W %h:%p' -A -D $port $user@$host")
echo "Executing --> $cmd"
eval "ssh ${cmd[@]}"
like image 36
Vijayant Avatar answered Feb 27 '26 02:02

Vijayant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!