How to excute .bash_profile when running the script from the remote host using "ssh -l <user_id> <server_name>" command?

Solution Verified - Updated -


  • I would like to run a script under a userid on a remote server but the user's environment variables are not set properly.
  • Looks like it is not running the user's ".bash_profile".
  • Is there any way I can set the user's environment variables before running the script?


Check the user's environment variables:

$ whoami

$ env|grep NUM

$ echo $NUM

$ cat ~/.bash_profile | grep NUM
export NUM=1999


 $ cat /home/user1/

 echo $NUM >> /home/user1/result.txt 2>&1

Run the script from the remote host via SSH:

 # ssh -l user1 /home/user1/  user1@'s password: 

Check the result:

 $ cat /home/user1/result.txt


--> The result shows the user's environment variable $NUM is not read.
How can I have it read the user's environment variable described in .bash_profile?


  • Red Hat Enterprise Linux 6
  • bash
  • openssh

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In