abhishek77in
abhishek77in

Reputation: 1896

Not able to declare a shell variable in a ssh session correctly

I am trying to create a directory on my remote server with the current timestamp. For which I need the following code to work.

#!/usr/bin/env bash

ssh -l pi something.com -p 8888 << EOF
  CURRENT_TIMESTAMP=`date +%s`
  echo "creating directory with timestamp $CURRENT_TIMESTAMP"
EOF

But when I see the output of the echo command the $CURRENT_TIMESTAMP value is completely missing. If I ssh into the remote machine and run each command one by one, everything works as expected.

Upvotes: 0

Views: 189

Answers (1)

F. Hauri  - Give Up GitHub
F. Hauri - Give Up GitHub

Reputation: 71017

Try with

ssh -l pi something.com -p 8888 << "EOF"
  CURRENT_TIMESTAMP=`date +%s`
  echo "creating directory with timestamp $CURRENT_TIMESTAMP"
EOF

and have a look at man -P'less +/<<' bash

... 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...

Upvotes: 3

Related Questions