I have a very strange configuration issue.
When logging to machine (SSH session) and executing the following commands using interactive shell, they succeed:
node --version
npm --v
However, when I execute the following through script from remote machine:
#!/bin/bash
#Script input arguments
user=${1}
server=${2}
#Tell the shell to quote your variables to be eval-safe!
printf -v user_q '%q' "$user"
printf -v server_q '%q' "$server"
#
# Script variables
address="$user_q"@"$server_q"
#
function run {
ssh "$address" /bin/bash "$@"
}
run << SSHCONNECTION
node --version
npm --v
exit
SSHCONNECTION
I get the following output:
/bin/bash: line 2: node: command not found
/bin/bash: line 3: npm: command not found
I am conscious that the issue may be the in .bash_profile file configuration
export PATH="/usr/local/bin/npm:/usr/local/bin/node:/usr/local/bin:$PATH"
Also, here is result of npm config get prefix:
/usr/local
Can you please suggest what I am doing wrong?
I believe what happened is due to the fact .bash_profile is only executed when bash is invoked for an interactive shell, more info about this here: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files
When invoking and passing the script directly to /bin/bash, it will behave as a non-interactive shell which the default behaviour to not read the .bash_profile.
I would suggest setting you PATH variable in /etc/environment so it will take affect for all users and all manners of scripting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With