ilya1725
ilya1725

Reputation: 4958

How to pass environment variables to bash on different machine

I need to run a shell script on a different machine. The general invocation method is like this:

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null bot-bono@other-dt 'bash' < $GIT_ROOT/chip/sw/scripts/build.sh

Due to other scripts being called by build.sh I can't pass parameters to it. AS far as I know if a bash is called locally it inherits the environment variables. That doesn't happen if it is called on a remote machine.

Done anyone know a way I can pass an environment variable to bash on a remote box? Or, perhaps there is a better way to invoke build.sh on a remote system that can use the local environment variables.

Upvotes: 0

Views: 480

Answers (1)

that other guy
that other guy

Reputation: 123560

The best way to do this is to use -o SendEnv=YOURVARIABLE, but it only works if the server is set to leniently allow variables. If it's not, here are possible workarounds:

You can set the variables in the command itself:

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null bot-bono@other-dt \
    "export CFLAGS='$CFLAGS' LDFLAGS='$LDFLAGS'; bash" < $GIT_ROOT/chip/sw/scripts/build.sh

This does not require any specific SSH permissions, but it does require escaping the variables if they could contain single quotes.

If you have a lot of variables and don't want to sort through them, you can dump entire environment along with your script:

{ set; cat "$GIT_ROOT/chip/sw/scripts/build.sh" } | \
    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null bot-bono@other-dt 'bash' 

This sets all your local variables and functions on the remote system, but could cause issues since it doesn't limit it to the variables you want, and overrides variables like PATH and DISPLAY.

Upvotes: 1

Related Questions