Understanding the PATH Variable in Bash

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

Environment variables track specific system information, such as the name of the user logged into the shell, the default home directory for the user, the search path the shell uses to find executable programs, and so on.
The PATH environment variable in Bash and other Unix-like operating systems such as Linux and macOS is a critical variable that dictates where the shell looks for executable files. When you type a command in the terminal, the shell searches through the directories listed in the PATH variable, in the order they are listed, to find the executable file that matches the command.
The directories in the PATH variable are separated by colons (:). You can view your current PATH setting by running the following command:
echo $PATH
For example, suppose PATH is set as follows:

When you run a command like ls, the shell will look for the ls executable in the following directories, in this order:
/usr/local/bin/
/usr/bin/
/user/local/sbin/
If it finds ls in /usr/bin/, for instance, it will run it from there and won't search in /user/local/sbin/.
Open a terminal.
Type which ls and press Enter.

The output shows the absolute path of the ls command. As we can see, this directory is the second one listed in the PATH directory.
Create a directory ~/scripts where you keep some of your custom scripts. To add this directory to your PATH as follows:
Open a terminal.
Run export PATH=$PATH:~/my_scripts
Confirm by running echo $PATH.

Note: This change is temporary and will be lost when you close the terminal. To make it permanent, you'll have to add the export command to your shell's startup file, like .bashrc or .bash_profile.
Create a new script file in the ~/scripts directory.
touch ~/scripts/test_command.sh
Make it executable.
chmod +x ~/scripts/test_command.sh

Edit the file to include the following:
#!/bin/bash
echo "Hello, this is my custom command. Let's check it!"
Save the file.
Try running test_command.sh from anywhere in the terminal. Did it work? If not, make sure that ~/my_scripts is in your PATH.

Run export PATH=$(echo $PATH | sed 's/:\/home\/centos\/scripts//')
This will remove /home/centos/scripts from your PATH (if it exists).
Confirm by running echo $PATH.
