linuxbashscriptingadministrative

Need an explanation of this scripting command


What does the command export PATH=$PATH:~/bin accomplish?

I would like to understand this more than I already do; please assist!


Solution

  • PATH is an environment variable that specifies directories to be searched (in order from left-to-right) to find executables. When you invoke something like gzip, the $PATH environment variable is split on : and each of those paths is searched to see if it contains gzip.

    It is common to prepend directories to this variable, so that they are searched before the existing (default) locations. This is generally done when you want to add a non-standard directory to the PATH, so that you can install applications to subdirectories.

    export PATH=$PATH:~/bin
    

    This appends ~/bin (i.e. "$HOME/bin") to the PATH, so that you can execute scripts/binaries from the "bin" folder in your home directory.

    You can determine which command will be executed from your PATH by using the which command. For example:

    -bash$ which gzip
    /usr/bin/gzip
    

    You can also drop the export keyword, but in doing this, the changed PATH variable will not be visible to scripts invoked from your bash shell.

    Take a look at the output of echo $PATH or env | grep PATH to see what that variable looks like.