Custom shell commands
Written: 1 Dec 2021 | Last updated: 17 Dec 2021
This guide shows you three ways to make custom, local shell commands on bash or zsh. The process may be similar on other Bourne Shell (sh) based shells.
I am still learning and so these may not be the best way make commands, but these are three ways which works.
Option 1: Using aliases
Aliases are an easy way of automating regularly used sequences of commands or just assigning a short command to run long commands.
They can also be used to make something run with a certain setting by default, for example make ls
run as ls -a -s -h
by default so that whenever you run ls
it shows you all files (including any hidden files) and their size in a human readable format.
To make an alias open your shell's configuration (rc) file.
See Find your shell's configuration file if you are not sure where or what that is.
Add your alias to the bottom of the file. The syntax for an alias is alias command="run this"
. Where command
is what you would like to enter into your terminal and run this
is what you would like it to run when you enter the command.
For example, if I wanted my earlier ls
configuration I would write:
alias ls="ls -a -s -h"
You can also use single quotes instead of double quotes if you would prefer.
Your alias also doesn't have to be at the bottom of your configuration file but it is recommended to help compartmentalise the custom configurations from the default ones, some OSs' even put comments at the bottom of the files to show you where to put custom commands.
You could use this to run scripts or other executables as commands, using alias command="/path/to/executable"
.
Finally, if you don't want to make a python script executable you could run a python script by using the python
or python3
command in your alias.
Option 2: Using the PATH variable
The PATH variable tells your shell where to check when a command is run.
You can see the current contents of your path variable using echo $PATH
. The different paths are seperated by commas.
This option requires you to have an executable to run.
2.1 Create an executable
The name of the executable must be the command that you would like to run, so unless you would like to write the file extension every time you run the command I'd avoid adding one. Your shell should work out the file type using file's shebang anyways.
If you would like an example executable write this shell script at ~/bin/exaCom
where exaCom
is the file name.
In that file write:
#!/bin/bash
echo "Example command ran"
Then save the file and make it an executable by running chmod +x path/to/file
.
2.2 Add the command to the PATH variable
First of all test this process by running:
export PATH=$PATH":$HOME/bin/exaCom"
Then try exaCom
. If it returns Example command ran
the process is working.
Open a new terminal.
Edit your shell's configuration file. For how to locate this see here.
Add the previous export
command to the bottom of the file. Save the file.
Open a new terminal and try the command. It should work.
Option 3: Using source
or .
3.1 Make the script
Open the directory ~/bin
. You could use a different directory if you wanted but I'd recommend using ~/bin
just so that you can keep your scripts together.
This can be done via terminal with $ cd ~/bin
.
If ~/bin
doesn't exist make it.
This can be done via the termial with $ mkdir ~/bin
.
Create the file using your favourite text editor.
In this example I use vim.
~/bin$ vim exampleScript.sh
Enter the following into your script:
#!/bin/bash
function exampleFunction(){
echo "Success"
}
Save the script.
3.2 Make the script executable
In the terminal enter:.
$ chmod +x exampleScript.sh
Test the file is now executable by running:
$ ./exampleScript.sh
You should not receive any error messages.
If you receive the error Permission denied
, try the chmod command again.
3.3 Make the script load into every terminal session automatically
Open your shell configuration file. See here if you aren't sure how to locate this .
Add source ~/bin/exampleScript.sh
to the end of the file (Can be on any line but putting it at the end makes it easier to find).
Alternatively replace source
with .
. It does the same thing, just is shorter.
Save the file.
3.4 Test the command
Open a new terminal and try exampleFunction
.
If you receive Success
the command works.
3.5 Optional: Run a python program from a command
This could be used for other languages too.
3.5.1 Find a way to run the program from the terminal
For a python file try:
$ python program.py
or
$ python3 program.py
3.5.2 Place that command inside the function in your script
function exampleFunction(){
python3 pathto/program.py
}
function exampleFunction(){
python3 pathto/program.py
}
Ensure the path is correct by running your command in the terminal whilst inside the directory holding the shell script.
I'd recommend creating a directory inside your bin
directory for python files.
3.5.3 Test the command
Open a new terminal and enter the command ($ exampleFunction
if you have been copying this guide).
Your python program should run.