Let's walk through the process of adding a fresh baby bash script as a command that you can run in your terminal.
Say you have a super rad script that does mega cool things somewhere on your computer:
#!/bin/bash echo Hello world
Allowing your bash terminal to run this script file as a command just takes a few simple steps.
I recommend creating a directory at
.bin. The bin folder is typically where commands go, so I'm going to replicate this:
$ cd ~/
$ mkdir .bin/
$ mv path/to/script ~/.bin/YOUR-SCRIPT-NAME
Add a line to your .bash_profile that will make sure your bash terminal has a path to this file when running your command.
If you don't have one create one with
$ touch ~/.bash_profile
#Here's the line you should add: export PATH=$PATH:/Users/YOUR-USERNAME/.bin
Now you can either restart your terminal at this point or source this file:
We'll use the illusive
chmod command to let users run this newly created command:
$ chmod u+x ~/.bin/YOUR-SCRIPT-NAME
And that's it! You should now be able to run your script from anywhere in your terminal.