2

I'm trying to define environment variable for non-interactive console in Raspbian.

In theory I need to add the varaibles to ~/.bashrc or ~/.profile or /etc/bash.bashrc or /etc/profile. For example I add this line (according to the file where I added it):

export VTEMP="set on ~/.bashrc"
export VTEMP="set on ~/.profile"
export VTEMP="set on /etc/bash.bashrc"
export VTEMP="set on /etc/profile"

If I write this lines in all files and logged via ssh, then putting the command: echo $VTEMP return: set on ~/.profile

If I go to the raspberry pi, open an LXTerminal and put these command echo $VTEMP return: set on ~/.bashrc

But if I try non-interactive remotely using ssh, writing: ssh pi@192.168.0.015 "echo $VTEMP > out.txt", when I go to see the content of the file out.txt there's nothing, it empty.

Why isn't VTEMP defined when using non-interactive?

Sgz
  • 23

2 Answers2

3

Short Answer

Try sourcing your rc file explicitly (and using single-quotes):

ssh pi@192.168.0.015 'source ~/.bashrc; echo $VTEMP > out.txt'

Explanation

There are two issues here. First, there's the issue of which rc (run commands) file is sourced under what circumstances. Second, there's the issue of what command you're passing to ssh.

Regarding the first issue, neither ~/.profile nor ~/.bashrc will be executed by a non-interactive Bash shell. In general, you might want to consider using the BASH_ENV environment variable when invoking non-interactive scripts. This is described in the Bash Manual here:

This issue is also discussed in the following posts, which you may want to consult:

Unfortunately for you, this may not be applicable in the case of executing a non-interactice ssh command - see the following post for further discussion:

So it seems that the best solution for you is probably to not rely on the automatic sourcing of one of the rc/profile files, and to just explicitly run the desired script yourself, e.g. run source ~/.bashrc before running echo $VTEMP > out.txt.

Finally, your command is using double-quotes instead of single-quotes, which means that your $VTEMP variable is being evaluated locally instead of remotely - you should use single-quotes instead. Putting all of this together, we have the following solution:

ssh pi@192.168.0.015 'source ~/.bashrc; echo $VTEMP > out.txt'
igal
  • 9,886
  • Thanks. My case is non-login and non-interactive shell. So i should put the enviroment variables on /etc/bash.bashrc, but before the line "[ -z "$PS1" ] && return". – Sgz Mar 10 '19 at 01:34
  • Althought the "source" command option for temporal one use enviroment variable looks more suitable. – Sgz Mar 10 '19 at 01:59
  • @Sgz Anything that you add to /etc/bash.bashrc will affect all users of the system, so use that with care. If you really just want to add a single variable to the environment before executing a command, you could also just do that, e.g. ssh user@host 'export VTEMP=whatever; echo $VTEMP. – igal Mar 10 '19 at 02:40
2

Your output is empty because you forgot to escape the command. It should be:

ssh pi@192.168.0.015 'echo $VTEMP > out.txt'

Otherwise the value of $VTEMP will be substituted by your shell locally and since you did not define it on your local machine it prints nothing.

tyrion
  • 153