Coding⏱️ 4 min read📅 2026-06-19

How to Fix: Tool written in Go works fine from terminal (in any directory) but gives "command not found" error when called from inside a script

A Go tool works fine from the terminal but gives a command not found error when called from a script.

Quick Answer: Check if the Go interpreter is in the PATH of the script's execution directory.

The 'command not found' error when running a Go tool from within a script can be frustrating, especially when the tool works fine in the terminal. This issue affects users who bundle multiple tools together into a single script and encounter this problem.

This error is particularly vexing because it implies that the tool is not installed or accessible, even though its interpreter is present in the system's PATH. However, we will explore possible reasons behind this behavior and provide steps to resolve the issue.

🛑 Root Causes of the Error

  • The primary reason for this error lies in how the operating system handles executable files. When you run a tool from the terminal, the operating system searches for it in the directories specified in your PATH environment variable. However, when you include the path to the tool in your script, the operating system only looks for the tool within that specific directory, ignoring the rest of the PATH.
  • Another possible cause is related to how the Go interpreter and the tool are packaged. If the tool is compiled as a binary, it may not be included in the same package as its interpreter, leading to issues when running the tool from a script.

🛠️ Step-by-Step Verified Fixes

Using Absolute Paths

  1. Step 1: To resolve this issue, you can use absolute paths to specify the location of your Go tool. Instead of including the path in your script, use the full path to the tool's executable.
  2. Step 2: For example, if your tool is located at /usr/local/bin/httprobe, you can modify your script to include the following line: `/usr/local/bin/httprobe -list `. This ensures that the operating system searches for the tool in the specified directory and ignores the rest of the PATH.
  3. Step 3: Additionally, consider using a shebang line at the top of your script to specify the interpreter to use. For example: `#!/usr/bin/env go` followed by the command to run your tool.

Using Relative Paths or Adding the Tool to the PATH

  1. Step 1: Alternatively, you can try using relative paths to specify the location of your Go tool. However, this method may not be reliable if the script is executed from a different directory.
  2. Step 2: Another approach is to add the path to the tool's executable to your system's PATH environment variable. You can do this by adding the following line to your shell configuration file (e.g., ~/.bashrc): `export PATH=$PATH:/usr/local/bin`.

✨ Wrapping Up

By understanding the root causes of the 'command not found' error, you can take steps to resolve the issue and ensure that your Go tool runs successfully from within a script. Remember to use absolute paths or modify your system's PATH environment variable as needed.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions