1. Introduction

We often install custom scripts and executables on our Linux systems. We’d prefer to use them conveniently by just typing the name of the script in the command line, regardless of our current working directory.

In this tutorial, we’ll see where we can place these scripts, both from technical and conventional perspectives.

2. The PATH Variable

On Linux systems, the PATH variable stores a list of directories with executables. When we try to run a command, the system checks for the executable with that name in all these directories.

Let’s view the contents of the PATH variable:

$ echo $PATH
/home/karthik/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/karthik/lib/android/cmdline-tools/tools/bin:/home/karthik/lib/android/emulator:/home/karthik/lib/android/platform-tools:/home/karthik/lib/flutter/bin:/usr/local/go/bin:/home/karthik/.spicetify

From the above output, we observe that the PATH variable contains a list of directories, delimited by a colon.

3. Adding Scripts to Conventional Directories

On most systems, some standard directories such as /usr/local/bin and ~/.local/bin are included in the PATH variable by default. Adding our script to any of these directories enables us to run the script from the command line.

When we include a script in any of the directories under /usr, it’s available system-wide. Therefore, all users can use it from the command line. So, for system-wide availability, we’d ideally place the script under /usr/local/bin.

For scripts intended for personal use, we’d be better off putting them in a directory under our home directory, ~, which is usually /home/<our-username>. The most straightforward location to place custom scripts for personal use is ~/.local/bin.

4. Adding Directories to PATH

We can also add directories to PATH, and then use the scripts under those directories from the command line.

For example, let’s include /home/karthik/.flutter/bin in the PATH variable:

$ PATH=$PATH:/home/karthik/.flutter/bin
$ export PATH

Once we append this directory to the PATH variable, all the scripts and executables from this directory will be available for use from the command line.

To avoid doing this on every reboot and make this addition permanent, we can run the above two commands automatically when the shell is initialized.

For system-wide availability, we need to include these lines in the /etc/profile file. And for personal use, we can include it inside of ~/.profile.

5. Conclusion

In this article, we learned where to place custom scripts to enable them to be used from the command line. We can add these scripts to one of the directories in the PATH variable, or append a directory containing our scripts to the PATH variable.

Ideally, we’d put scripts for system-wide use somewhere under /usr and those for personal use under our home directory.

Comments are closed on this article!