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

Solution Verified - Updated -

Issue

  • 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?

Example:

Check the user's environment variables:

$ whoami
user1

$ env|grep NUM
NUM=1999

$ echo $NUM
1999

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

Script:

 $ cat /home/user1/test.sh
 #!/bin/bash

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

Run the script from the remote host via SSH:

 # ssh -l user1 10.10.10.10 /home/user1/test.sh  user1@10.10.10.10'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?

Environment

  • 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