Display or print UNIX / Linux path ~ $PATH variable

in Categories , , , , , , , , last updated August 24, 2017

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 "%s\n" $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 "%s\n" $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
Fig.01: Printing $PATH on Linux or Unix-like system

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

Share this on (or read 8 comments/add one below):

8 comment

  1. slight improvement:


    path ()
    {
    local IFS=:
    eval printf "%s\\\n" \$${1:-PATH}
    }

    accepts an optional argument that is the name of a path-like variable.

    1. Hi,
      I did not get either of those path() working. I assume my version of unix does not recognize IFS when applying printf. But this works:

      echo $PATH | sed ‘s/\:/\n/g’ | sort

      1. Thanks for that, Pekka. I started using zsh, which has the same problem you described. The original solution works wonderfully in bash, but yours works in zsh.

      2. Ugghh curly quotes. Could not copy and paste.

        I am using Zshell, Prezto, OSX. The sed command replaces the : with an n

        To get one path per line I used:

        echo “${PATH//:/$’\n’}”

  2. Hello Sir;
    I am trying to run a program on a cluster and every time I run the program I have this message: mpiexec was unable to launch the specified application as it could not find an executable. ”
    so I am suggesting that the program is not recognize the mpixec path. so I need to add the MPI path in my working directory.
    my question is how to set this up?

  3. hola necesito decargar un editor ok lo descargo en superusuario y cuando esta descargando me dice q no puede continuar … me aparece esto : dpkg: aviso: `ldconfig’ no se ha encontrado en el PATH o no es ejecutable.
    dpkg: aviso: `start-stop-daemon’ no se ha encontrado en el PATH o no es ejecutable.
    dpkg: error: 2 expected programs not found in PATH or not executable.
    Note: root’s PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
    E: Sub-process /usr/bin/dpkg returned an error code (2)
    Un paquete no se pudo instalar. Tratando de recuperarlo:
    dpkg: aviso: `ldconfig’ no se ha encontrado en el PATH o no es ejecutable.
    dpkg: aviso: `start-stop-daemon’ no se ha encontrado en el PATH o no es ejecutable.
    dpkg: error: 2 expected programs not found in PATH or not executable.
    Note: root’s PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
    help me..

  4. In below code:
    Node=/liferay-portal-5.2.0/jboss-tomcat-4.2.3/server/node_portal
    export jboss=$Node/../..
    echo $jboss

    Output :

    /liferay-portal-5.2.0/jboss-tomcat-4.2.3/server/node_portal/../..

    But I need Output as :

    /liferay-portal-5.2.0/jboss-tomcat-4.2.3.

    Please help me out in this.

    Have a question? Post it on our forum!