Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How have both local and remote variable inside an SSH command

Tags:

linux

bash

How can I have both local and remote variable in an ssh command? For example in the following sample code:

A=3; ssh host@name "B=3; echo $A; echo $B;" 

I have access to A but B is not accessible.

But in the following example:

A=3; ssh host@name 'B=3; echo $A; echo $B;' 

I don't have A and just B is accessible.

Is there any way that both A and B be accessible?

like image 880
Sepehr Samini Avatar asked Dec 11 '12 18:12

Sepehr Samini


People also ask

What is SSH remote command?

The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.

How to run command using SSH on a remote machine?

How to Run Command Using SSH On Remote Machine? There are different ways to run multiple commands on a remote Unix server using SSH. This article shows the most straightforward and easy approach to SSH and runs multiple commands in using the bash shell. ssh user@server sudo apt update sudo: no tty present and no askpass program specified

Can you run multiple commands on SSH?

SSH: Run Multiple Remote Commands. In the most cases it is not enough to send only one remote command over SSH. Much more often it is required to send multiple commands on a remote server, for example, to collect some data for inventory and get back the result.

What is ssh command in Linux?

SSH: Execute Remote Command or Script – Linux. This is quite a common task for Linux system administrators, when it is needed to execute some command or a local Bash script from a one Linux workstation or a server on another remote Linux machine over SSH.

How do I run multiple commands on a remote UNIX server?

There are different ways to run multiple commands on a remote Unix server using SSH. This article shows the most straightforward and easy approach to SSH and runs multiple commands in using the bash shell. ssh user@server sudo apt update sudo: no tty present and no askpass program specified


1 Answers

I think this is what you want:

A=3; ssh host@name "B=3; echo $A; echo \$B;" 

When you use double-quotes:

Your shell does auto expansion on variables prefixed with $, so in your first example, when it sees

ssh host@name "B=3; echo $A; echo $B;" 

bash expands it to:

ssh host@name "B=3; echo 3; echo ;" 

and then passes host@name "B=3; echo 3; echo ;" as the argument to ssh. This is because you defined A with A=3, but you never defined B, so $B resolves to the empty string locally.


When you use single-quotes:

Everything enclosed by single-quotes are interpreted as string-literals, so when you do:

ssh host@name 'B=3; echo $A; echo $B;' 

the instructions B=3; echo $A; echo $B; will be run once you log in to the remote server. You've defined B in the remote shell, but you never told it what A is, so $A will resolve to the empty string.


So when you use \$, as in the solution:

\$ means to interpret the $ character literally, so we send literally echo $B as one of the instructions to execute remotely, instead of having bash expand $B locally first. What we end up telling ssh to do is equivalent to this:

ssh host@name 'B=3; echo 3; echo $B;' 
like image 80
sampson-chen Avatar answered Oct 01 '22 22:10

sampson-chen