I have a file with some lines in it:


Now i'm trying to use each line with a while IFS loop to do certain actions with them.

while IFS=' ' read -r test || [ -n "${test}" ];
    if ssh root@$TARGET "[ ! -d $test ]"
        echo "${test}"
done < $filelist

The problem here I only get one result and it's not looping through the whole file? The if statement should be correct, since there are no directories with these names.

If however i remove the entire if part and just use

echo "${test}"

the loop works.


ssh inherits the standard input stream from the while loop and will read your whole $filelist file, apart from its first line which is read by read.

To stop ssh from doing this, use ssh -n, or ssh ... </dev/null.

ssh works this way to to allow you to pass data via a simple redirection or pipe to the remote system, as in

ssh remote 'cat >remotefile' <localfile

See also "Need help understanding shell command with pipes, redirections and remote connections".

  • Thanks! Never imagined SSH is the culprit!
    – Slimo
    May 4 '20 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.