Introduction to automating shell commands
There is a lot that can be accomplished efficiently with Bash.
After getting started working with Bash you’ll quickly discover commands that you run again and again.
For some simple commands you can make them even more convenient by creating a shell alias or a shell function.
Other commands involving multiple steps you can typically use and manage more efficiently by creating a shell script.
Getting started with shell aliases, shell functions, and shell scripts in Bash
You can find very understandable introductions to each approach at the links below:
If you just need a refresher on the specific commands for each, than you can refer to the sections below.
Making a Bash alias
Bash alias form
To create an alias named “alias-name” type:
alias alias-name='command'
To use the alias type:
alias-name
Bash alias example 1
To create an alias named “l1” to call the ls command with the option to show one file per line type:
alias l1='ls -1'
To use the alias type:
l1
Bash alias example 2
To create an alias named “quickcd” to navigate to a specific nested directory type:
alias quickcd='cd ~/dir1/dir2/dir3'
To use the alias type:
quickcd
Making aliases persistent
The above examples will only impact the current shell session. To make those changes permanent for Bash on Ubuntu you’ll likely want to include them in the ~/.bashrc
file. (See the note on system-specific updates.)
Making a Bash function
Bash function form
To create a function named “function-name” type:
function-name () {
commands
}
To use the function type:
function-name
Bash function example 1
To create a function named “datetime” to call the date command with the output formatted a particular way type:
datetime () {
date '+The current date and time is: %Y-%m-%dT%T%:z'
}
To call the function enter the function name type:
datetime
Bash function example 2
To create a function named “wisecow” to pass the output of the fortune command to the cowsay command type:
wisecow () {
fortune | cowsay
}
To call the function enter the function name type:
wisecow
Making functions persistent
The above examples will only impact the current shell session. To make those changes permanent for Bash on Ubuntu you’ll likely want to include them in the ~/.bashrc
file. (See the note on system-specific updates.)
Making a Bash script
Bash script process
Step 1: Create the file
Create a file with the “.sh” extension:
touch example-script.sh
Step 2: Write the script
Edit the file to include “#!/bin/bash” on the first line followed by the desired commands on subsequent lines:
#!/bin/bash
<command 1>
<command 2>
<command 3>
<command ...>
<command N>
Step 3: Make the script executable
Alter the file permissions to make the script executable.
chmod u+x example-script.sh
Step R1: Run the script
Run the script by entering “./” before it’s name:
./example-script.sh
Bash script example 1
To make a script that prints “Hello everyone!":
Step 1: Create the file
Create a file with the “.sh” extension:
touch script-hello.sh
Step 2: Write the script
Edit the file to include the following:
#!/bin/bash
echo 'Hello everyone!'
Step 3: Make the script executable
Modify the file permission to make it executable:
chmod u+x script-hello.sh
Step R1: Run the script
Run the script:
./script-hello.sh
Bash script example 2
To make a script to run htop with output sorted by memory usage:
Step 1: Create the file
Create a file with the “.sh” extension:
touch script-htopmem.sh
Step 2: Write the script
Edit the file to include the following:
#!/bin/bash
htop –sort PERCENT_MEM
Step 3: Make the script executable
Modify the file permission to make it executable:
chmod u+x script-htopmem.sh
Step R1: Run the script
Run the script:
./script-htopmem.sh
Making a Bash script executable from anywhere
The above example scripts can only be run from within the directory containing them or by including that directory when calling the script.
In order to make a Bash script available to run anywhere on our system we first need to complete the three steps above:
- Step 1: Create the file
- Step 2: Write the script
- Step 3: Make the script executable
Then we need to complete two more:
- Step 4: Store the script in an appropriate directory
- Step 5: Update the $PATH to include that directory
Finally, we can run the script with a simpler form:
- Step R2: Run the script
These additional steps are outlined below.
Step 4: Store the script in an appropriate directory
Move the script to the directory where you want to store it.
There are numerous considerations for deciding where to store scripts that are beyond the scope of this article.
For this example let’s say that you want to put it in the “/home/user/scripts” directory and that you have moved your example-script.sh file there.
Step 5: Update the $PATH to include the directory
Ensure the directory is in the $PATH
environment variable so your shell can find the script when you call it.
export PATH=$PATH:/home/user/scripts
The above command will only impact the current shell session.
To make these changes permanent for Bash on Ubuntu you’ll likely want to include them in the ~/.bashrc
file. (See the note on system-specific updates.)
Step R2: Run the script
Now you can execute this script anywhere simply by entering the script file name as a command:
example-script.sh
Note on system-specific updates
To make many of these changes persistent you’ll need to update the appropriate system file(s). Which file(s) you need to update depends on your operating system and your shell.
The examples referenced here are for Bash on Ubuntu, where you’ll likely want to update the ~/.bashrc
file.
Other operating systems and other shells may require updating a different set of files.