0

I want to ssh into a remote host, and then execute a for loop that goes through sequence of numbers to control number of different nodes.

ssh user@host /bin/bash << EOF
for i in {1..10}
do
echo $i
done
EOF

If I do this, the output is just 10 blank lines, instead of printing out numbers from 1 through 10.

If I execute same code on my local machine, I get the desired output which is ten lines each line printing from 1 through 10.

How would one achieve the intended functionality, that is accessing the index in a for loop that is being executed in SSH?

1
  • 1
    did you try eval? I suspect the $i is expanded locally, before sending it over via ssh. Aug 20, 2014 at 21:16

1 Answer 1

1

From the bash manual:

Here Documents
The format of here-documents is:

<<[-]word
        here-document
delimiter

[...] If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion.

Let's try it:

$ i='Howdy!'
$ ssh user@localhost /bin/bash << EOF
for i in {1..10}
do
echo $i
done
EOF
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
Howdy!
$ ssh user@localhost /bin/bash << 'EOF'
for i in {1..10}
do
echo $i
done
EOF
1
2
3
4
5
6
7
8
9
10
$

You must log in to answer this question.

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