When executing this bash script, it only shows my local path.
ssh ${REMOTE_HOST} 'bash -s' <<EOL
set -e
source ~/.profile
echo $PATH
# Commands here don't work because $PATH is not set properly.
# How can I see what $PATH is set to here?
EOL
How can I view the remote value of $PATH to debug this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The $PATH is getting expanded prior to running on the remote server.
Example #1
Say I run these commands from a system called skinner.bubba.net.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f7eaeaf1c5f6eeecebebe0f7">[email protected]</a> ~]# ssh mulder 'bash -s' <<EOL > echo $HOSTNAME > hostname > EOL skinner.bubba.net mulder.bubba.net
By moving the single quote so that the echo $HOSTNAME is inside it, you can guard the variable from getting expanded by skinner’s Bash shell.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34465b5b4074475f5d5a5a5146">[email protected]</a> ~]# ssh mulder 'bash -s <<EOL > echo $HOSTNAME > hostname > EOL' mulder.bubba.net mulder.bubba.net
Example #2
The other method would be to escape the $HOSTNAME with a slash, which tells Bash you want to send a literal dollar sign.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7c61617a4e7d656760606b7c">[email protected]</a> ~]# ssh mulder 'bash -s' <<EOL > echo $HOSTNAME > hostname > EOL mulder.bubba.net mulder.bubba.net
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0