Skip to content

Bash SSH Command Not Found in Script Fix

DodaTech Updated 2026-06-24 2 min read

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 (~/.bashrc or ~/.bash_profile) at the start of remote commands.
  • Use ssh -t for 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

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [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

### Why does SSH not source my .bashrc?

Non-interactive SSH commands (without -t) do not source .bashrc or .bash_profile. The shell runs in a minimal mode. Use source ~/.bashrc explicitly in the command string.

What is the difference between bash -l and bash without -l?

bash -l (login shell) sources ~/.bash_profile, ~/.bash_login, and ~/.profile. Non-login shells source ~/.bashrc. SSH commands typically run as non-interactive non-login shells.

How do I transfer environment variables over SSH?

Pass variables in the command string: ssh user@server "VAR=$VALUE; command". Or use env to pass variables: ssh user@server "env VAR=$VALUE command". For sensitive data, use SSH config or ~/.ssh/environment.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro