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?
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.
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With