Setting Environment Variables in Remote Bash Shells Spawned with SSH

7,684

Solution 1

Use the env command:

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

Solution 2

This is tested to work. But I'm not certain if this is the way you want it.

ssh username@hostname 'FOO=14 BAR=giraffe bash -s' < script.sh
Share:
7,684

Related videos on Youtube

Dave
Author by

Dave

Updated on September 18, 2022

Comments

  • Dave
    Dave over 1 year

    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?