I'm running this SSH command:
ssh -p 2001 -q root@12.13.14.15 \
" echo
echo Now On Target Server, I see
ls -l /tmp/Folder-1.1.0.1053/*
for v in A B C ; do
echo === $v===
done
echo
for f in /tmp/Folder-1.1.0.1053/* ; do
echo File is == $f
done
"
and it's printing this:
Now On Target Server, I see
-rw------- 1 root root 159790 Jan 23 17:03 /tmp/Folder-1.1.0.1053/file1-1.8.30.tar.gz
-rw------- 1 root root 116731 Jan 23 17:03 /tmp/Folder-1.1.0.1053/file2-2.7.49.tar.gz
=== ===
=== ===
=== ===
File is ==
File is ==
I have a few questions:
- Why doesn't the first
for
loop print the values of the variable$v
? - Why (when I see the target server have valid files in
/tmp/<folder>
) the secondfor
loop did not print the values of the$f
variable? - How can I get the second
for
loop to print the two.tar.gz
files? - I tried
$(...)
or using back quotes to wrap the input values (forfor
loop) in the secondfor
loop but it has no effect. For some reason, it's expecting those*
files using the local machine. Why?
If it's relevant, my Bash version is "GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)", and my Linux version is "Red Hat Enterprise Linux Server release 6.9 (Santiago)".
Because you're wrapping your ssh command with double quotes, all your
$
variables are being interpolated by the local shell before going over the wire to the remote shell. Either use single quotes or escape your$
sigils.Ex:
or
See very Important note below. Escape only the variable created and used within the SSH session (ex:
for
loop here) or any where inside SSH session. Do NOT escape a variable which was defined in your script and outside of SSH session, ex: as used in the command / input part infor
loop. i.e.for v in ${staging_area}/*.tar.gz; do echo ==\$v;done"
here I escaped\$v
but did not escape${staging_area}