I'm using bash, and I want to be able to execute a script just by typing its name as a command, same as pwd
for example.
Is there a specific directory where I need to save my script to, or any other system files I need to edit to achieve this?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou can check what locations are currently checked for direct commands by looking at the $PATH
variable:
echo $PATH
It's likely this includes /usr/local/bin
, in which case you could put a symbolic link there:
ln -s /opt/mysuperscript /usr/local/bin/mysuperscript
Now you can just type mysuperscript
to run your script.
In addition to making sure the script is in the $PATH, you also must make the script executable. chmod +x SCRIPTNAME
is how you do that.
You have to install that script in one of the directories of $PATH
. Use (echo $PATH
) to see the directories of $PATH
That means either copy the script to
Or make a symbolic link to the script inside one of the directories of $PATH
Or append the script directory to $PATH
export PATH=$PATH:<script directory>
You may want to check the ln
command. It can be used to create a link to a file or directory.
Try this link to get more infomation: ln command example
/usr/bin
or/usr/local/bin