1
#!/bin/bash -x

ssh -T root@jumpbox ssh -T root@IP << EOF  
YY=$(date +'%y')  
echo $YY  
EOF  

OUTPUT:

Trying tunnel from jumpbox...  
Pseudo-terminal will not be allocated because stdin is not a terminal.  
-bash: line 6: YY: command not found

The variable I need to assign is more complex, it's not about year number as shown in my description.

  • Firstly you can't have spaces around the '=' in bash variable assignments. Secondly the output doesn't match the snippet. Thirdly -T actively suppresses terminal allocation, it doesn't mean "tunnel" if that's what you're hoping for. All up this is a very poorly written question. How about you tell us what you're actually trying to achieve and post a self-contained coherent test case? – tink Sep 4 '17 at 8:44
4

When you don't quote the EOF in <<EOF, expansions are perfomed by the local shell inside the here document (so the $(date ...) is expanded by the local date and $YY is expanded to the value in the local shell (empty unless you have assigned a value earlier)). If you the content of the here-document to be passed verbatim to the remote shell, you need to quote the EOF one way or another:

ssh -T root@IP << 'EOF'
YY=$(date +'%y')
echo "$YY"
EOF  

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.