I would like to connect through SSH and run a command upon connecting.
I run the following command:
ssh host -t "command"
but i need to change some environmental variables in order to run the command and it seems like eval is not working.
ssh host -t "eval `somescript.sh tcsh`; env | grep variables_that_should be_changed"
It simply doesn't eval the script and says that the command does not exist "somescript.sh".
The somescript.sh exists on the remote machine, not the one im sshing from.
Is there a way to solve this?
Put setting the environment variables and executing the command in the same script and run that script over ssh. The environment variables will be valid until your script exists.
You've already selected a "best answer", so my answer is just helping you understand what was wrong.
Based on the code in your question, it seems that you're experiencing a quoting problem. You said:
ssh host -t "eval `somescript.sh tcsh`; env | grep variables_that_should be_changed"
When you have backticks (`...`
) inside double quotes, they get expanded by the calling shell rather than the shell you're running on the remote server. So for your command line, somescript.sh
would need to exist locally, and you'd use your command line to embed the output of that script into the command line you're running remotely. Probably not what you want.
Notwithstanding other syntactical or procedural improvements, if you replace your double quotes with single quotes, then the backticks will be sent as part of the command line you're running remotely, rather than expanded locally.
Also, if you use backticks, consider replacing them with $(...)
notation instead. It is easier to read, can be nested, and are generally considered the "better way" with modern shells.