Passing a variable via ssh does not work

I am trying to pass a variable through an ssh connection, for example:

working_dir="/home/user/some_dir/"

ssh $USER@some _host 'qsub $working_dir/some_file.txt'

The connection is established, but this code gives me the following error:

working_dir: Undefined variable.

This can be explained by the fact that there is no $working_dir variable on the remote computer, since it was defined locally.

Is there a way to get the value in a command locally?

+4
source share
2 answers

Try using double quotes that should evaluate the variable locally:

ssh $USER@some _host "qsub $working_dir/some_file.txt"

+11
source

You are using a single quote string - and I suppose the variables are not interpolated inside them.

What if you try to use a double quote string ?
Example:

 ssh $USER@some _host "qsub $working_dir/some_file.txt" 

In this case, the $working_dir variable should be interpolated at your end - and its value sent via the ssh connection.

+2
source

Source: https://habr.com/ru/post/1303669/


All Articles