Reputation: 1618
I'm using the following:
filename="Test File 17-07-2020.xls"
sshpass -p $password ssh root@$IP /bin/bash -s "$filename" << 'EOF'
echo $1
EOF
This works when filename equals Testfile.xls
and the echo outputs the full filename.
But fails if the filename is called Test File 17-07-2020.xls
My understanding is the spaces are breaking the input so it becomes:
$1 = Test
$2 = File
$3 = 17-07-2020.xls
Is there anyway to pass this keeping the spaces and having it all in $1
If I add filename=$(echo "$filename" | sed 's/ /\\ /g')
before the SSHPASS command it does work.
Is that a valid way to do it or is there a better way ?
Thanks
Upvotes: 0
Views: 444
Reputation: 530960
You still need to quote $1
. Quoting the delimiter prevents $1
from being expanded early; it doesn't prevent word splitting once the shell actually executes echo $1
.
sshpass -p $password ssh root@$IP "/bin/bash -s \"$filename\"" << 'EOF'
echo "$1"
EOF
If you are using bash
locally, there are two extensions that could produce a value that is safe to pass to the remote shell.
sshpass -p $password ssh root@$IP "/bin/bash -s $(printf '%q' "$filename")"
or
sshpass -p $password ssh root@$IP "/bin/bash -s ${filename@Q}"
The former works at least in bash
3.2 (I highly doubt you are using an older version), while the latter requires bash
4.4.
Upvotes: 1