Linux – Setting Environment Variables in Remote Bash Shells Spawned with SSH

bashlinuxopensshssh

I often execute commands like this:

ssh username@hostname 'bash -s' < bashScriptToExecuteRemotely

I now have a case where bashScriptToExecuteRemotely is expecting certain environment variables to be set. In my case, let's say they are FOO=14 and BAR=giraffe.

How may I cause the bash shell that's launched on the remote host to have these environment variables set before executing bashScriptToExecuteRemotely?

Best Answer

Use the env command:

ssh username@hostname env VAR1=VALUE1 VAR2=VALUE thecommand the args
Related Question