How To Set Path In Linux Terminal And Bash Scripts

You Typed a Command and Linux Said “Command Not Found”

You’ve just installed a new tool, maybe Node.js, Python’s pip, or a custom script. You open your terminal, type the command you know should work, and are met with a cold, dismissive error: “bash: command not found”.

This is one of the most common friction points for anyone moving beyond basic Linux usage. The system isn’t broken, and your installation likely succeeded. The issue is almost always the PATH environment variable. Your shell simply doesn’t know where to look for that new executable.

Mastering the PATH is a foundational skill. It unlocks the ability to run your own scripts from anywhere, manage multiple software versions, and keep your system organized. This guide will walk you through exactly what the PATH is, how to view it, and multiple methods to set it correctly—both temporarily and permanently.

What Exactly Is the PATH Variable?

Think of your Linux shell as a librarian. When you ask for a book (a command like “ls” or “python”), the librarian needs to know which shelves to check. The PATH variable is that list of shelves—a colon-separated list of directories where the shell looks for executable files.

When you type “ls”, your shell searches each directory in the PATH, in order, for a file named “ls” that has execute permissions. It runs the first one it finds. This is why you can run core utilities like “ls” and “cd” from any directory without specifying their full location (e.g., /bin/ls).

Common default PATH directories include:

/usr/local/bin

/usr/bin

/bin

/usr/local/sbin

/usr/sbin

/sbin

User-installed software often goes into /usr/local/bin or ~/.local/bin. Your own scripts might live in ~/bin.

First Step: Checking Your Current PATH

Before you change anything, you need to see what you’re working with. Open a terminal and use the echo command.

echo $PATH

The output will be a single line with directories separated by colons. It might look something like this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

You can also use the printenv command for a clearer view of this specific variable.

printenv PATH

Take a moment to scan the list. Note the order. The shell searches from left to right. If you have two different versions of a program in two different PATH directories, the one in the leftmost directory will be used.

Which Shell Am I Using?

The method for making PATH changes permanent depends on your shell. The most common is Bash. To check, run:

echo $SHELL

If it reports /bin/bash or /usr/bin/bash, you’re using Bash. This guide focuses on Bash, but the concepts are similar for Zsh (which uses ~/.zshrc) or Fish (which uses ~/.config/fish/config.fish).

How to Temporarily Set the PATH in Your Current Session

Sometimes you only need a command available for your current terminal window or a single script. Temporary changes are perfect for testing.

To add a single directory to the front of your PATH for the current session, use this command.

export PATH=”/home/yourusername/myapps:$PATH”

This syntax prepends /home/yourusername/myapps to the existing PATH. The shell will check this directory first. To add it to the end instead (giving it lowest priority), reverse the order.

export PATH=”$PATH:/home/yourusername/myapps”

You can verify the change immediately with echo $PATH. This modification will last only until you close that terminal window or start a new shell session. It’s ideal for quick tests without affecting your system’s global configuration.

how to set path in linux

How to Permanently Set PATH for Your User in Bash

For changes that persist across every new terminal you open, you need to modify your shell’s startup configuration file. For Bash, this is typically the .bashrc file in your home directory.

Open the file in a text editor like nano.

nano ~/.bashrc

Scroll to the bottom of the file. Add your export command. A good practice is to add custom user directories to the front.

export PATH=”$HOME/.local/bin:$HOME/bin:$PATH”

This adds your personal ~/.local/bin and ~/bin directories to the PATH. The $HOME variable expands to your home directory path, making the command portable.

After saving the file and exiting the editor (in nano: Ctrl+X, then Y to confirm, then Enter), you must load the changes into your current session. You can either close and reopen your terminal or use the source command.

source ~/.bashrc

Now, any executable placed in ~/.local/bin or ~/bin will be available from any terminal you open.

Using .profile or .bash_profile for Login Shells

You might also encounter ~/.profile or ~/.bash_profile. These files are read by “login shells” (like when you log in via SSH or a tty). For most desktop users where terminals are “non-login” shells, .bashrc is sufficient.

To be thorough and ensure your PATH is set in all scenarios, you can add the same export line to your ~/.profile file. Often, .profile will source .bashrc anyway. Check if your ~/.profile has a line like this.

if [ -n “$BASH_VERSION” ]; then
if [ -f “$HOME/.bashrc” ]; then
. “$HOME/.bashrc”
fi
fi

If it does, then changes in .bashrc will be picked up by login shells as well.

Setting PATH System-Wide for All Users

If you are an administrator and need to make a program available to every user on the system, you can modify system-wide configuration files. This requires sudo privileges.

Common system-wide files are /etc/environment and files within /etc/profile.d/.

The /etc/environment file is not a script but a simple list of assignments. You can add a PATH line, but you must include the full, expanded PATH value.

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/myapp/bin”

A more modular approach is to create a shell script in /etc/profile.d/. For example, create a file called myapp-path.sh.

sudo nano /etc/profile.d/myapp-path.sh

Inside, add the export command.

export PATH=”$PATH:/opt/myapp/bin”

Files in /etc/profile.d/ are sourced by all users’ login shells. Changes here will take effect the next time users log in.

Use system-wide changes sparingly and only for software that truly needs to be globally accessible.

Best Practices for Managing Your PATH

A messy PATH can lead to confusion, security issues, and unexpected behavior. Follow these guidelines to keep it clean and functional.

Prepend personal directories, not system ones. Adding your ~/.local/bin to the front is safe. Prepend system directories like /usr/bin can break core system commands if you accidentally place a script with the same name there.

Be mindful of order for version management. If you use tools like pyenv or nvm to manage Python or Node.js versions, they work by inserting their shim directories at the front of your PATH. Ensure your PATH modifications in .bashrc don’t interfere. Usually, you should source nvm/pyenv initialization after your basic PATH export, or let them handle it entirely.

Avoid adding the current directory (.) to your PATH. While convenient, it’s a significant security risk. A malicious script named “ls” placed in a directory you cd into could be executed instead of the real /bin/ls. Instead, run local scripts with ./scriptname.

Keep it clean. Periodically review your PATH with echo $PATH | tr ‘:’ ‘\n’ to list directories on separate lines. Remove directories for software you’ve uninstalled.

how to set path in linux

Organizing with Helper Functions in .bashrc

For advanced management, you can create Bash functions to temporarily modify your PATH. This is useful for project-specific toolchains.

Add this to your ~/.bashrc.

add_to_path() {
if [ -d “$1” ]; then
export PATH=”$1:$PATH”
echo “Added $1 to PATH”
else
echo “Directory $1 does not exist”
fi
}

Then in your terminal, you can simply run add_to_path “/path/to/project/tools”.

Troubleshooting Common PATH Problems

Even after setting the PATH correctly, things can go wrong. Here’s how to diagnose frequent issues.

Command still not found after export. First, verify the directory you added actually contains the executable. Use ls -la /path/you/added. Ensure the file has execute permissions (chmod +x filename).

Changes to .bashrc aren’t taking effect. Did you remember to run source ~/.bashrc? Check for syntax errors in the file by running bash -n ~/.bashrc. Also, ensure you’re not modifying .bashrc in a different user’s home directory.

The wrong version of a command runs. This is a PATH order issue. Use which command to see which executable is being found first.

which python

To see all instances in your PATH, use the type command with the -a flag.

type -a python

This will list all “python” executables found in the PATH, in the order they are checked. Adjust your PATH order accordingly.

Permission denied errors. This isn’t a PATH issue but a file permissions issue. The shell found the file, but you aren’t allowed to execute it. Fix it with chmod +x.

Putting It All Into Practice

Let’s walk through a concrete example. You’ve downloaded a standalone application called “mycli” and extracted it to ~/apps/mycli. The executable is inside that folder.

First, you could run it directly with the full path.

~/apps/mycli/mycli

To make it available via just “mycli”, you need to add its directory to your PATH. The permanent, user-specific method is best.

Open your ~/.bashrc file and add this line at the end.

export PATH=”$HOME/apps/mycli:$PATH”

Save the file, then source it.

source ~/.bashrc

Now, from any directory, you can simply type mycli and it will run. For even better organization, you could create a symbolic link from a directory already in your PATH, like ~/.local/bin.

ln -s ~/apps/mycli/mycli ~/.local/bin/mycli

This is often cleaner than polluting your PATH with many single-app directories.

Your PATH to Linux Mastery

Understanding and controlling the PATH variable transforms your relationship with the Linux command line. It moves you from a passive user following instructions to an active organizer of your own environment.

Start by making your ~/.local/bin directory the home for your personal scripts and manually installed utilities. Use temporary exports for experimentation. Respect the order of precedence to avoid conflicts. With these skills, errors like “command not found” become simple puzzles you can solve in seconds, not frustrating roadblocks.

The terminal is a powerful workshop, and a well-configured PATH is the blueprint that keeps all your tools within easy reach.

Leave a Comment

close