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?

2 Answers 2


Use the env command:

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .