12

The following does not work for me:

ssh [email protected] "k=5; echo $k;"

it just returns an empty line.

How can I assign a variable on a remote session (ssh)?

Note: My question is not about how to pass local variables to my ssh session, but rather how to create and assign remote variables. (should be a pretty straight forward task?)


Edit:

In more detail I am trying to do this:

bkp=/some/path/to/backups
ssh [email protected] "bkps=( $(find $bkp/* -type d | sort) );
                        echo 'number of backups: '${#bkps[@]};
                        while [ ${#bkps[@]} -gt 5 ]; do
                            echo ${bkps[${#bkps[@]}-1]};
                            #rm -rf $bkps[${#bkps[@]}-1];
                            unset bkps[${#bkps[@]}-1];
                        done;"

The find command works fine, but for some reason $bkps does not get populated. So my guess was that it would be a variable assignment issue, since I think I have checked everything else...

4
  • 1
    Hint try the command locally first or login to the ssh server and try it. Bash requires you to use the export command to set environment variables. ssh [email protected] "export k=5; echo $k;" Commented Oct 23, 2012 at 14:17
  • @Codeguy007: I expanded my decription of my problem more. I am not actually trying to set any environment variables. I assume the simplified problem has something to do with redirecting output actually... Commented Oct 23, 2012 at 14:27
  • 3
    Try ssh [email protected] "k=5; echo \$k;". Note the backslash before the $ to prevent the shell from expanding early. Single quotes instead of double quotes would accomplish the same thing. Commented Oct 23, 2012 at 14:50
  • @twalberg : Your '\$k' solution worked :) Thank you very much! Although if I'd use single quotes instead of double quotes, my local variable would not be transferred to the ssh session. (I'll accept your answer if you type it up as an answer (: ) Commented Oct 23, 2012 at 15:06

2 Answers 2

26

Given this invocation:

ssh [email protected] "k=5; echo $k;"

the local shell is expanding $k (which most likely isn't set) before it is executing ssh .... So the command that actually gets passed to the remote shell once the connection is made is k=5; echo ; (or k=5; echo something_else_entirely; if k is actually set locally).

To avoid this, escape the dollar sign like this:

ssh [email protected] "k=5; echo \$k;"

Alternatively, use single quotes instead of double quotes to prevent the local expansion. However, while that would work on this simple example, you may actually want local expansion of some variables in the command that gets sent to the remote side, so the backslash-escaping is probably the better route.

For future reference, you can also type set -x in your shell to echo the actual commands that are being executed as a help for troubleshooting.

Sign up to request clarification or add additional context in comments.

2 Comments

could please explain how to use the "set -x" command? is it part of the ssh command or just bash?
Just adding: this also goes for $(command) -> \$(command) when you want it to be executed remotely. - It took me some time to realize this so I hope this reminder helps someone in the future, or myself when I have forgotten ;)
0

beside escaping with \, you can also use single quote.

  • "": means use local env variable
  • '': means use remote env variable

In your case,

ssh [email protected] "k=5; echo $k;"

Should be change to:

ssh [email protected] 'k=5; echo $k;'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.