./ and $PATH

When using a standard shell command it's usually typed straight on to the command line after the prompt and executed by hitting enter. But when a shell script is run it is often preceded by a dot and a forward slash ./

 

In the case of the former standard commands, Bash tries to find the typed command from a series of directories that are stored in a variable called $PATH, which can be seen by typing echo $PATH which returns an output such as:

 

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

 

You can see that each directory is separated by a colon :

 

Since the $PATH variable is individual to each user on a system, it can be set to suit each specific user.

 

Accordingly, for convenience sake, one could add a specific directory (e.g. ~/myscripts) for your custom shell scripts that would therefore not require the preceding ./

 

Temporary path addition:

 

export PATH=$PATH:~/myscripts

 

Permanent path addition - add the above to the bottom of your ~/.bashrc file:

 

export PATH=$PATH:~/myscripts

 

Now, if you were to save all your shell scripts in ~/myscripts, you'd no longer need to preceded with them with ./ to run them.