Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run while loop with read through ssh:


ssh root@ "cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c \"%Y %n\" "$path"  >> /tmp/fs_10.10.10.10.log done"

But the issue is my variable $path is resolving on my localhost where as I want to resolve it on remote host so that it can read file on remote host and take stat of all folder/files listed in "rm_filesystem.log"

share|improve this question

or put the ssh command line into single quotes rather than double. then the command will be passed as is without any substitution at the local end. i.e.

ssh root@ 'cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c "%Y %n" "$path"  >> /tmp/fs_10.10.10.10.log done'
share|improve this answer

ssh user@host "bash -s" <

What is currently happening is that the script is being parsed on your local machine and then sent in its entirety to the remote machine. So by the time it gets there, the $PATH variable will have already been replaced by your local $PATH value.

What the example above does is open a bash session on your remote machine and set bash to read from stdin (see

You then pass in the script (which you have typed into an external file) so that it executes directly on the remote machine.

This way, there is no local parsing of the script, and thus no replacement of the environment variable.

share|improve this answer
up vote 0 down vote accepted

I made it working in a good way as it should be:


ssh root@ "cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c '%Y "%n"' "\"'$path'\"" >> /tmp/fs_10.10.10.10.log done"

After reading bash man page I got this trick ..

Thanks everyone for help

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .