1

I'm running this SSH command:

ssh -p 2001 -q [email protected] \
  " 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:

  1. Why doesn't the first for loop print the values of the variable $v?
  2. Why (when I see the target server have valid files in /tmp/<folder>) the second for loop did not print the values of the $f variable?
  3. How can I get the second for loop to print the two .tar.gz files?
  4. I tried $(...) or using back quotes to wrap the input values (for for loop) in the second for 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)".

1 Answer 1

3

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:

ssh -p 2001 -q [email protected] "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"

or

ssh -p 2001 -q [email protected] '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'

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 in for loop. i.e. for v in ${staging_area}/*.tar.gz; do echo ==\$v;done" here I escaped \$v but did not escape ${staging_area}

Sign up to request clarification or add additional context in comments.

3 Comments

Zeeesus! Thanks.
So very important NOTE here.. If using double quote with SSH, make sure a user is escaping $v or $f type of variables i.e. any variable within the for or similar loop. You Must NOT backslash variables using in the conditional / command part of the for loop itself. Ex: for v in ${staging_area}/*.tar.gz; do echo \$v; done here I didn't escape staging_area variable or it'll leave //*.tar.gz as for loop's input.
Don't keep adding to the answer - your comments are fine, they'll still be readable to later people.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.