I am executing a command on remote machine with -t flag ( to prevent the command from 'blocking' ) the prompt (somehow it works that way). So I am using ssh -nq -t root@ip but with this, the script cannot find my environment variable that I use.

Why is this happening and how can I avoid this ?



The information below is valid if you ssh into the machine and then run your command which starts a login shell:

ssh root@ip

If you are trying to access the variable directly from the ssh command (ssh root@ip echo $XXXX_LICENSE_FILE) then you are starting a non-login, non-interactive shell in which case neither ~/.bash_profile nor ~/.bashrc are read. From the bash man page:

   When bash is started non-interactively, to run a shell script,
   for  example,  it looks for the variable BASH_ENV in the envi‐
   ronment, expands its value if it appears there, and  uses  the
   expanded  value  as  the  name  of a file to read and execute.
   Bash behaves as if the following command were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to  search  for
   the file name.

The easiest workaround would be to simply declare the variable along with the ssh command. For this to work, you need to add this line to the remote /etc/ssh/sshd_config file:

PermitUserEnvironment yes

Then run this from the local machine:

XXXX_LICENSE_FILE=/etc/xxxx.lic && ssh -nq -t -t -t root@$ip /etc/init.d/xxxx.rc start

The problem here is the difference between login and interactive shells. See here for a nice summary.

~/.bashrc is only read when starting an interactive (non-login) shell. When you sshinto a remote machine, you are running a login shell so ~/.bash_profile or ~/.bash_login or ~/.profile (in that order) are read instead. You can check this by running bash after loging on to the remote server, that will start an interactive shell, ~/.bashrc will be read and your environment variable will be set.

To fix this, define the variable in your ~/.bash_profile instead of ~/.bashrc.

I am assuming that you are setting the variable in the remote .bashrc, not your local one. If you want to export a local variable over ssh, have a look at the link posted by Frederik Deweerdt. The easiest way would be to include your variable in the invocation of ssh:

ssh -nq -t root@ip "FOO=foo BAR=bar"
  • Even after defining the variable in .bash_profile system can't find the variable.
    – Patryk
    May 7 '13 at 14:48
  • @Patryk please explain exactly what you are doing. How is the variable defined and where (local .bash_profile, or remote?) and how are you trying to access its contents. Is the remote shell bash?
    – terdon
    May 7 '13 at 15:46
  • I have the variable both in .bashrc and .bash_profile defined as follows export XXXX_LICENSE_FILE=/etc/xxxx.lic, then I have in script ssh -nq -t -t -t root@$ip /etc/init.d/xxxx.rc start
    – Patryk
    May 8 '13 at 6:33
  • 1
    OK. You mean the remote /root/.bash_profile right? Are you sure the remote shell is bash?
    – terdon
    May 8 '13 at 11:19
  • Yes, I am root:x:0:0:root:/root:/bin/bash. And yes - the remote one.
    – Patryk
    May 8 '13 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.