Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: using EXPORT inside SSH command

Tags:

bash

ssh

When executing this command:

user@local:~ >ssh user@remote " export myvar=myvalue ; echo myvar=$myvar ; "

I get output:

myvar=

When running directly on remote machine, I get the expected result:

user@remote:~ > export myvar=myvalue ; echo myvar=$myvar ;

Output:

myvar=myvalue

So how to set a variable inside ssh command?

like image 636
przno Avatar asked Sep 27 '13 13:09

przno


People also ask

What is Bash export command?

Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.

Can I SSH in a bash script?

Bash script SSH is a common tool for Linux users. It is needed when you want to run a command from a local server or a Linux workstation. SSH is also used to access local Bash scripts from a local or remote server.

What is export in shell script?

You can use the export command to make local variables global. To make your local shell variables global automatically, export them in your . profile file. Note: Variables can be exported down to child shells but not exported up to parent shells.

Is Bash a substitute for SSH?

Show activity on this post. Bash is a shell, replacing sh, which was common before. SSH is a protocol for secure connections. tcsh is an alternative to bash.


1 Answers

That's because you're using double quotes, so the variable is being expanded on your local machine before ssh is even invoked. Use single quotes:

ssh user@remote 'export myvar=myvalue ; echo myvar=$myvar'
like image 133
glenn jackman Avatar answered Oct 12 '22 21:10

glenn jackman