Search

Making an executable available in all folders: export


There are situations when some executables or scripts are present in a certain forlder and we end up changing directory right up toll that folder every time to execute it. This is obviously not a very optimal way of doing it.


For e.g. let us say we have a script "hello.sh" in

/home/user/Desktop/temp/hello.sh


Every time we open the terminal, it opens in /home/user and to execute hello.sh we have to change directory to Desktop/temp.


To able to execute hello.sh from any folder in the system we will have to tell the system about the path where hello can be found.


The environment variable PATH stores the information regarding the various folders that the sytem looks into when we execute a command in the folder. Only if the executable is found in any of the folders listed in the PATH , the sytem is able to execute it successfully from any where in the system.


To look at what the value of PATH in your system is , run the command





Now to be able to execute hello from any where in the system, we need to add the the path to the folder containing hello.sh into $PATH.


This can be done using the export command as follows






Please note that the $PATH before ":" is very important as we only want to add the value of our
folder to $PATH and not replace the already existing values.



Now if we execute hello.sh from any folder it should work with out any problems. But an export  of this kind is valid only  in the terminal in which we execute the export command and only as long as the terminal is open. Once the terminal is closed and reopened, the value of $PATH is back to its default value.



To make sure that the value of $PATH stays updated every time a terminal is opened we can add the same command to the file   ~/.bashrc



This is file that every terminal looks into when it is launched and any command in this file gets executed before the terminal starts working.



Thus open ~/.bashrc in any editor and add the same export command that we executed above at the end of the file and save it.


Now close and reopen the terminal and you should be able to execute hello.sh form any where.
Note: hello.sh should have execute permissions


Follow by Email