Bash SSH Command Not Found in Script Fix
In this tutorial, you'll learn about Bash SSH Command Not Found in Script Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Bash SSH commands fail with command not found when running commands remotely because the remote shell does not source profile files in non-interactive mode.
The Wrong Way
ssh user@server "npm --version"
Output:
bash: npm: command not found
The remote shell may not have npm in its PATH because profile files are not sourced in non-interactive SSH sessions.
The Right Way
ssh user@server "source ~/.bashrc; npm --version"
Output:
10.2.3
Source the profile file explicitly to set up the PATH on the remote server.
Step-by-Step Fix
1. Source the profile before the command
ssh user@server "source ~/.bashrc && npm --version"
2. Use the full path to the command
ssh user@server "/usr/local/bin/npm --version"
3. Set the PATH explicitly
ssh user@server "export PATH=\$PATH:/usr/local/bin && npm --version"
4. Use interactive SSH mode
ssh -t user@server "npm --version"
5. Source a script that sets up the environment
ssh user@server "bash -l -c 'npm --version'"
Prevention Tips
- Use full paths to executables in remote SSH commands.
- Source the profile file (
~/.bashrcor~/.bash_profile) at the start of remote commands. - Use
ssh -tfor interactive sessions that source profile files. - Use
bash -l(login shell) which sources profile files automatically. - Check which PATH the remote shell uses with
ssh user@server 'echo $PATH'.
Common Mistakes with ssh command
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world BASH code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro