user3033194
user3033194

Reputation: 1821

Enclosing a command in escape characters in bash

I have a command in quotes:

 ssh -i "/home/$USER/$KEY" "$USER"@"$WORKER1IP"   "echo 'if [[ `uname -a` == Darwin* ]]; then' >> /home/$USER/tachyon-0.5.0/conf/tachyon-env.sh"

The "uname" part has to be written literally, not executed. I tried using "\" before the 'uname' part, but I don't know how to close it. Can someone help please?

Upvotes: 0

Views: 65

Answers (3)

Jahid
Jahid

Reputation: 22428

This should work:

ssh -i "/home/$USER/$KEY" "$USER"@"$WORKER1IP"   "echo 'if [[ \"\$(uname -a)\" == Darwin* ]]; then' >> /home/$USER/tachyon-0.5.0/conf/tachyon-env.sh"

and this too:

ssh -i "/home/$USER/$KEY" "$USER"@"$WORKER1IP"   "echo 'if [[ \`uname -a\` == Darwin* ]]; then' >> /home/$USER/tachyon-0.5.0/conf/tachyon-env.sh"

Upvotes: 1

chepner
chepner

Reputation: 530990

Use a here document instead, which simplifies the nested quotes.

ssh -i "/home/$USER/$KEY" "$USER@$WORKER1IP" <<EOF
    echo 'if [[ $(uname -a) == Darwin* ]]; then' >> "/home/$USER/tachyon-0.5.0/conf/tachyon-env.sh"
EOF

Upvotes: 1

anubhava
anubhava

Reputation: 785008

You can use $(...) with escaped $ in BASH:

ssh -i "/home/$USER/$KEY" "$USER"@"$WORKER1IP"   "echo 'if [[ \$(uname -a) == Darwin* ]]; then' >> /home/$USER/tachyon-0.5.0/conf/tachyon-env.sh"

Upvotes: 1

Related Questions