Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Unable to retrieve the profile variable using ssh.

server2 ==> user apptst ==> bash profile has $APPHOME variable.

Was trying to retrieve the value from server1 using ssh but failed.

Used options below

server1> ssh apptst@server2 'echo $APPHOME'

server1> ssh apptst@server2 echo '$APPHOME'

server1> ssh apptst@server2 echo "$APPHOME"

server1> ssh apptst@server2 "echo $APPHOME"

when tried

server1> ssh apptst@server2 ls -ld $APPHOME

it works fine.

Any suggestions what is missing and how can I get the variable from server2?

share|improve this question
In the second case the shell on server1 expands $APPHOME, and not server2. Check e.g. with cd / && ssh apptst@server2 'echo $PWD' (you need these quotes ', otherwise again shell on server1 will expand), which will return / and not ~. – mpy Mar 15 '13 at 13:19
Hi Thanks for the explanation. But all the cases have failed to provide the output. confused completely what mistake I am doing. – KumarJohn Mar 15 '13 at 14:37
up vote 4 down vote accepted

This command should work:

 server1> ssh apptst@server2 'bash -l -c "echo \$APPHOME"'
  • you need ' quotes to pass that complete command to ssh as one argument; this also prevents the shell on server1 to expand $APPHOME itself
  • bash -l starts a login shell, which reads in the profiles files, where $APPHOME gets defined (as @chepner pointed out in his answer, this is not default when invoked by sshd)
  • -c executes the next argument, again with quotes that the complete echo command gets executed.
  • escaping \$ is necessary, because otherwise the parent (non-login) shell which sshd invokes (and which inherits the environment diretly from init (*)) does expand $APPHOME. But this is too early as $APPHOME only gets defined for login shells (via ~/.profile). [added this point after comment from OP]

(*) correct me, if I'm wrong here.

share|improve this answer
Hi Thanks for the reply... I tried as suggested but still I am getting a blank output only. – KumarJohn Mar 15 '13 at 14:25
@user2119462: This becomes really weired, I think I can reproduce your issue now. Try this really ugly workaround: ssh apptst@server2 'bash -l -c "set | grep ^APPHOME"' – mpy Mar 15 '13 at 14:50
@mpy...thanks for the workaround it worked fine. But still could not understand why the first solution is not working – KumarJohn Mar 15 '13 at 15:06
@user2119462 Did you try the improved answer? I'm really interested if that works now, too! – mpy Mar 15 '13 at 15:07
@mpy...the Improved answer worked. Thanks a lot – KumarJohn Mar 16 '13 at 8:43

The problem is that when you specify an exact command to run via ssh, the remote sshd does not start a login shell to execute it, but a regular non-interactive shell. In that case, .bash_profile is not executed, and so APPHOME is not defined.

And while typing this, mph has posted the correct solution to force a login shell that processes .bash_profile.

share|improve this answer
Thanks Chepner.....but there is no output. I tried to assign it to a variable but still was having the same issue, blank output. – KumarJohn Mar 15 '13 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .