Sunday 29 December 2019

linux - Unable to get profile variable using ssh


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?



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...