Bash Command Hierarchy & PATH Explained (Part 2 of 34)





Every time you type a command in the Linux terminal, you need to be familiar with both the Bash PATH explained and the actual Bash command lookup process. For those interested in writing scripts, fixing "command not found" errors, or adding your own tools to your system, understanding Bash's command lookup process is critical. This article explains the entire process by which Bash determines what program to execute when you press Enter after entering a command. Additionally, this article addresses the method of controlling command execution via the Linux PATH variable.

Why This Matters:

Have you ever seen one of these frustrating errors as a Linux beginner?

  • command not found even though you installed the tool
  • A script runs fine in one folder but fails in another
  • You run ls and get colored output but you never added any options

All of these are connected to how Bash searches for commands. Once you understand PATH and command hierarchy, these problems become easy to solve.

Before you begin using the terminal, it's helpful to get familiar with some basics first. Check out our Linux commands for beginners prior to reading this article.

#01

What Happens When You Type a Command in Bash?

The majority of new users have the incorrect assumption that when they type ls, then press Enter, bash immediately locates and executes the /bin/ls executable. However, bash has a strict order of finding commands and searches many types of commands prior to looking for them as executables on the disk.

There are five categories (types) of commands defined by The Bash Command Hierarchy. Bash looks through these five categories in the same specific order:

bash
LinuxTeck.com
1. Alias
2. Function
3. Shell Built-in
4. Keyword
5. File

Explanation:

Alias is a shortcut name stored in memory. For example ls might actually be ls --color=auto behind the scenes.

Function is a custom function defined in your shell session or .bashrc.

Shell Built-in means commands that are built directly into Bash itself, like cd, echo, and pwd.

Keyword means reserved words like if, for, while, and case.

File is an actual executable file found by searching the directories listed in PATH.

Bash stops at the first match it finds. So if an alias named ls exists, Bash runs that alias and never checks for the actual /bin/ls file. This is why knowing the lookup order matters when you write bash scripts.

#02

Using the type Command to Identify Command Types

Bash has a built-in command called type that tells you exactly what kind of command a word is and which file or definition will be used when you run it.
This is one of the most useful tools for understanding what is happening in your terminal.

Basic usage:

bash
LinuxTeck.com
type ls
OUTPUT
ls is aliased to `ls --color=auto'

This tells you that ls is actually an alias. That is why your file listings show colors by default even though you never typed any extra options.

To see all matches for a command including the alias and the actual file location, use the -a flag:

bash
LinuxTeck.com
type -a ls
OUTPUT
ls is aliased to `ls --color=auto'
ls is /bin/ls

To get just the type label as a single word, which is useful when checking command types from inside a script, use the -t option:

bash
LinuxTeck.com
type -t ls
type -t cd
type -t if
type -t pwd
OUTPUT
alias
builtin
keyword
builtin

The output is simple and clean, which is exactly what a script needs when it checks command types programmatically.

You can also check multiple commands at the same time:

bash
LinuxTeck.com
type ls pwd do id

Tip:

If Bash finds a function when you run type, it will also print the full function definition. This is very helpful when you are debugging unexpected behavior in your terminal session. You can learn more about useful terminal tools in our useful Linux terminal commands guide.

#03

Bash PATH Explained: What is the PATH Variable in Linux?

When you type a command that is not an alias, function, built-in, or keyword, Bash needs to find the actual executable file on your disk.
It does this by searching through the directories listed in the Linux PATH variable.

PATH is an environment variable that holds a list of directory paths separated by colons.
Bash reads this list from left to right and stops as soon as it finds a matching executable file.

bash
LinuxTeck.com
echo $PATH
OUTPUT
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Each directory is separated by a colon. Bash checks each one in order until it finds the executable or reports command not found if nothing matches.

Important:

The current working directory is not included in PATH by default. Linux will only check for executables in PATH directories. If you want to run a script that is sitting in your current folder, you must use ./scriptname to tell Bash exactly where to look. This is by design and not a bug.

#04

How to Add Your Current Directory to PATH

It is possible to include the current directory in PATH by adding it to the PATH variable.
This means your scripts in the current folder can be run without typing ./ in front of them.

bash
LinuxTeck.com
export PATH=$PATH:.

The dot at the end represents the current directory. Each directory in PATH is separated by a colon, so this simply appends the current directory to whatever PATH already contains.
Now your PATH is updated to include the current working directory, and each time you change directories your scripts in that folder can be run easily.

Common Mistake:

Many beginners write export PATH=/my/dir instead of export PATH=$PATH:/my/dir. This wipes out all existing directories in PATH and causes almost every command to break with command not found.

Fix: export PATH=$PATH:/your/new/directory — always include $PATH: to keep the existing directories intact.

#05

Using the which Command to Find Executables

The which command in Linux is a quick way to find out exactly which file will be executed when you run a command. It searches only the directories listed in your PATH and prints the full path of the first match it finds.

bash
LinuxTeck.com
which bash
which python3
which ls
OUTPUT
/bin/bash
/usr/bin/python3
/bin/ls

This is particularly useful when you have multiple versions of the same tool installed and want to confirm which one will actually run.

Note — which vs type:

which only searches PATH for files. It does not know about aliases, functions, or built-ins. For a complete picture of what Bash will actually run including aliases and built-ins, use type -a commandname instead.

Use which when you want to find the physical file path, and type when you want to understand how Bash will interpret a command. A good example is cdwhich cd returns nothing because cd is a shell built-in, not a file on disk. But type cd correctly tells you it is a built-in.

You can read more about the cd command in Linux to understand this better.

#06

Practical Example — Organizing Your Own Scripts with PATH

Now that you understand how PATH works in Bash, let us put it all into practice. The best habit for any Linux user who writes scripts is to keep them in a dedicated folder and add that folder to PATH. This way you can run your scripts by name from any directory, just like system commands.

Here is a complete beginner-friendly walkthrough:

bash
LinuxTeck.com
# Step 1: Create your personal scripts folder (only if it does not exist)
test -d $HOME/bin || mkdir $HOME/bin

# Step 2: Add HOME/bin to PATH
export PATH=$HOME/bin:$PATH

# Step 3: Create a simple test script
cat > $HOME/bin/greet.sh << 'EOF'
#!/bin/bash
echo "Hello from $USER! Today is $(date +%A)."
EOF

# Step 4: Make it executable
chmod +x $HOME/bin/greet.sh

# Step 5: Run the script by name from anywhere
greet.sh

OUTPUT
Hello from john! Today is Sunday.

Explanation:

The line test -d $HOME/bin || mkdir $HOME/bin uses a conditional statement directly at the command line. It reads as: if the directory does not exist, then create it. The use of $HOME instead of a hardcoded path like /home/john means this command works for any user regardless of their username or current directory.

This is a good example of how bash scripting and automation can be applied directly at the command line too, not just inside script files.

Tip:

To make this PATH change permanent so it applies every time you open a new terminal, add the export line to your ~/.bashrc file and then run source ~/.bashrc. You can also check our Linux shell scripting command cheat sheet to keep handy commands in one place as you continue learning.

Common Mistake:

Forgetting to make your script executable before trying to run it by name. Without chmod +x, Bash cannot execute the file and will throw a Permission denied error.

Fix: chmod +x $HOME/bin/yourscript.sh — always run this after creating a new script. You can verify with ls -l $HOME/bin/ and confirm the x permission bit is set.

Check our chmod command in Linux guide for a full explanation of file permissions.

FAQ

Interview Questions (Practical)

What is the Bash command lookup order and why does it matter?

When you type a command, Bash searches in this strict order: Alias, Function, Shell Built-in, Keyword, and then File via PATH. Bash stops at the first match. This matters because a custom alias or function with the same name as a system command will always run first. Knowing this order helps you diagnose unexpected behavior and write more predictable bash scripts.

How do you check what type of command something is in Linux?

Use the built-in type command. To see the type label only, use type -t commandname. To see all matches including alias, built-in, and file, use type -a commandname. For example:

bash
LinuxTeck.com
type -a echo
OUTPUT
echo is a shell builtin
echo is /bin/echo
What is the difference between which and type in Linux?

which searches only PATH directories for executable files. It does not know about aliases, functions, or shell built-ins. If you run which cd, it returns nothing because cd is a built-in. type on the other hand checks all command types including aliases and built-ins. Use which to find a binary file path. Use type -a to understand exactly what Bash will run.

How do you permanently add a directory to PATH in Linux?

Add an export line to your ~/.bashrc file so it applies every time a new terminal session starts:

bash
LinuxTeck.com
echo 'export PATH=$HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

After running source ~/.bashrc or opening a new terminal, the new directory will be part of PATH in every future session.

Why does Linux not include the current directory in PATH by default?

The current directory is not searched unless it is explicitly added to PATH. This is a deliberate design choice. It ensures that commands you type always run the expected system binary and not a file placed in your current folder by accident or by someone else. If you need to run a script from the current directory, you must be explicit and use ./scriptname.

How do you create a personal scripts folder and make it accessible from anywhere?

Create a bin directory inside your home folder and add it to PATH. The following command creates the directory only if it does not already exist:

bash
LinuxTeck.com
test -d $HOME/bin || mkdir $HOME/bin
export PATH=$HOME/bin:$PATH

Once this is done, any script placed inside ~/bin can be run by name from any directory on the system. You can find more scripting tips in our Linux shell scripting interview questions guide.

END

Summary

Understanding Bash PATH explained and the Bash command hierarchy gives you a clear picture of how Linux finds and runs commands.
Bash always searches in the order Alias, Function, Built-in, Keyword, and then File via PATH.

The PATH variable controls where that final file search happens, and you can customize it to include your own scripts folder using export PATH=$HOME/bin:$PATH.

Once your ~/bin folder is set up, you are ready to start writing scripts. Check out our what is bash scripting in Linux article to continue your learning journey.

Related Articles


LinuxTeck — A Complete Linux Learning Blog
Learn step-by-step how to automate Linux tasks with real-world scripts and practical examples.

About Sharon J

Sharon J is a Linux System Administrator with strong expertise in server and system management. She turns real-world experience into practical Linux guides on Linux Teck.

View all posts by Sharon J →

Leave a Reply

Your email address will not be published.

L