Display or print UNIX / Linux path ~ $PATH variable

I am a new Linux or Unix system user and I am using a Debian Linux VPS. How do I print current path settings under BASH or sh or ksh shell?

In Linux or Unix-like file systems, the human-readable address of a resource is defined by PATH. On Unix / Linux like operating systems, (as well as on DOS / Windows and its descendants), PATH is an environment variable listing a set of paths to directories where executable may be found.

Display current PATH

Use the echo command as follows:
$ echo $PATH
Sample output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games

You can use the printf command as well to show the current PATH settings:
$ printf "%sn" $PATH
Sample outputs:

/Users/veryv/google-cloud-sdk/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/veryv/bin:/opt/X11/bin:/Applications/Wireshark.app/Contents/MacOS

Modify current PATH

Use the export command to add /opt/games to PATH, enter:
export PATH=$PATH:/opt/games
To format your PATH variable for easy viewing, add following code to your bash startup file (such as ~/.bashrc or ~/.bash_profile) :

function path(){
    old=$IFS
    IFS=:
    printf "%sn" $PATH
    IFS=$old
}

function path(){
old=$IFS
IFS=:
printf "%sn" $PATH
IFS=$old
}

Now just run path:
$ path
Output:

/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/bin/X11
/usr/games

Another option is to run the following command:
echo $PATH | tr ":" "n" | nl
Sample outputs:

Fig.01: Printing $PATH on Linux or Unix-like system

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.