Useful Bash Functions For Developers

Bash is the default shell for most Linux operating systems today. As a software engineer, I believe that bash has a lot to offer to software developers regardless of the language in which they program. Using bash scripts can save you a ton of time through automation of different tasks that are usually manually performed. Today I shall introduce you to the basics of bash scripting and some of the useful bash functions that you can use to automate small tasks as developers.

What is a Shell?

useful bash functions for developers

For those of you who are not familiar with bash, The shell is an Interface to your *nix Operating System. It gives you an environment to execute commands and programs (scripts) and receive the output of the executed commands or programs. The most commonly used Shells include:

  • Bourne Shell (sh)
  • Bourne Again Shell (bash)
  • C Shell (csh)
  • Z Shell (zsh)
  • korn Shell (ksh)

Today we shall focus on the Bash shell which is very popular among major Linux distros.

Getting A Bash Script Up & Running

Bash scripts are files that you can execute in a shell. Shell scripts have a .sh extension. To make your own shell script all you need to do is create a file with.sh extension, modify permissions to make the file executable and run it. Here is a hello world Bash script:

#!/bin/bash
echo "Hello World"

Save the above lines in a file and save it as hello.sh. To make the script executable, run the following command in the terminal:

sudo chmod +x hello.sh

Now to run the script, use the following command:

./hello.sh

Voila! you just created your first shell script. Now we shall learn to make use of bash functions to make your scripts more useful.

Special Bash Variables

Variable
Name
Description
$$Process id (pid) of the currently running bash script.
$nHolds the arguments passed in while calling the script
or arguments passed into a function inside the scope of
that function.
e.g: $1, $2… etc.,
$0The filename of the currently running script.

Useful Bash Functions

1. Basic Navigation & File Functions

I’ve decided to put the basic functions in a single section because as linux users, you are probably already familiar with the basic functions and you’re here to learn something new.

Function NameDescription of the functionExample Usage
cdchange directorycd /var/www
lslist files inside the working directoryls /opt
cpcopy a filecp file1.rb file1_copy.rb
mvMove / Rename a filemv abc.txt /opt/xyz.txt
rmRemove filerm -rf /opt/chrome
echoprint to the terminalecho “Hello World”
catprint out the contents of a filecat hello.txt
mkdircreate a direcorymkdir hello
touchcreate a file or update the last
edited/updated time of the file
touch hello.txt
chmodchange file permissionschmod a+x test.sh
chownchange file ownershipchown -R $USER /opt

2. cURL

cURL is a program that can help you make http requests and receive the response in your script. cURL can be used in your scripts in various ways. An example would be calling the ifconfig.io to get your current public IP Address.

curl ifconfig.io

cURL is preinstalled on a lot of operating systems. But if your distro doesn’t have it installed, you may install it using your package manager. If you happen to use the apt package manager, the command for installing cURL is

sudo apt-get install curl

3. df

the df command shows you the disk usage of all the mounted filesystems on your machine. You can also use df with files / directories as arguments to check their size on disk.

4. gawk

gawk is a drop in replacement tool for grep which is used to search through files or to find matching text in outputs of other programs/scrips. gawk is much faster than grep and color codes the matching text to make lookups easier. To search through files, use:

awk search_term name_of_directory/

5. mount / umount

The mount and umount command can be used to mount and unmount filesystems on your machine. You can include it in your bash scripts to automate copying or backing up files to your data partition.

mount /dev/sda5 /media/portable_volume

6. Rsync

Rsync is an extremely important tool for developers. It helps sync files across remote locations with ease. It has a plethora of options to finetune the way files are synced across devices / remote locations. Rsync can also be used to create backups on your devices locally. I will feature a separate article on Rsync as its scope of usage is wider. The basic usage format for rsync is as follows:

rsync options source destination

7. read

Getting user input in your bash scripts is a very useful function that you’ll need while customising your bash scripts. The read function lets you accept input from users at runtime. Here’s how you do it

read name_of_variable
echo $name_of_variable

8. cron

cron is not a function. It is a utility that lets you schedule the periodic execution of your scripts. Cron on its’ own is a very intricate subject and you’ll need to spend some time to learn about its options and capabilities. The crontab is a file that stores all the script locations and the times at which it needs to be run. You can edit the crontab using the following command:

crontab -e

To list the already existing cron jobs, you need to run the following command:

crontab -l

You can learn more about cronjobs here.

9. systemctl

systemctl command/utility allows you to run and stop various daemon services on your computer like your database engine or apache. It is important to know this function as it can be very useful while writing automation scripts. You can use the following options to start, stop or restart a service like apache:

sudo systemctl start|stop|restart apache2

10. ps

The ps function will output an entire list of processes and related details running at a particular point in time. This is specifically useful when you need to kill a specific process or to get the process id (pid) of a process. using ps in conjunction with grep can help you achieve the desired results. An example of using the ps command with grep would be:

ps aux | grep puma

The above code will return a list of all the running puma instances along with their process ids. If you have multiple processes of the application running, you can get all their process ids using ack by running the following:

ps aux | grep chrome | awk '{print $2}'

11. sed

sed or Stream EDitor lets you change the output of a datastream as opposed to already existing files. You can make use of regular expressions to identify and replace parts of the datastream as it is generated before being written to a file or the output stream. The sed command can be used in the following way:

ps aux | grep ir | sed 's/sbin/www/'

The above code will get the list of running processes with “ir” in its name and replace all instances of the word “sbin” with “www” in the output stream.

12. md5sum

The md5sum method generates an md5 checksum for a file which you can compare with the md5 checksum that was provided to you at the source from which you downloaded the file to ensure that the file you have downloaded has not been manipulated or injected with malicious code. This can be useful for verifying the integrity of files you share or receive. To generate the md5 checksum for a file, run:

md5sum filename.txt

13. xargs

xargs is a wonderful tool that is quite important while writing automation scripts. xargs accepts one argument which is the name of a command or function and passes in everything that is piped to the xargs command to the program that is called by xargs. An example for this would be:

ps aux | grep chrome | awk '{print $2}' | xargs kill

The above script would scan through the processes list generated by the ps aux command and find all the chrome processes from which the awk command would print only the process ids (PIDs) which is then passed on to the xargs function which will call the kill function and pass in all the piped arguments to the kill function. Sounds intimidating? It isn’t. Try it out yourself until you get the hang of it.

14. GNU Parallel

useful bash functions for developers

Scripts are generally run serially when called. This means that only after the completion of one function, the next one is called. This is okay for most tasks but if you need to speed up the process and the functions are not dependent on the completion of their preceding functions, then there is a way to parallelize your scripts. GNU Parallel is a project that helps you parallelize your scripts/tasks.

To install GNU Parallel, you need to run the following code in your terminal emulator:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

To learn more about the parallel utility, you can visit their project website.

15. Notify Send

notify-send is a command line utility that lets you send push notifications to your desktop. You can integrate it with shell scripts to notify you of various events when running tasks in the background. Based on your package manager, you may use any of the following commands to install the notify-send utility.

# APT (Ubuntu / Debian)
sudo apt-get install libnotify-bin

# YUM (Cent OS / RHEL)
sudo yum install libnotify

# DNF (Fedora)
sudo dnf install libnotify

Now to test your installation, you may run the following code and you’ll see that a notification popup has been pushed:

notify-send "Hello World"

If you’d like to read more about functions, tips & tweaks specific to Ubuntu, you can checkout my article on Ubuntu Command Line Tips & Tricks.

That was my list of some of the most useful bash functions for developers. You can use combinations of these functions in different ways to create scripts and automate your everyday non-core tasks as developers to save a ton of your time.

Shell scripting is a vast subject. Its a scripting language on its own with control structures and intricacies. But this article should help you get started. I shall cover this subject more in detail in my future articles. So stay tuned.

Sreedev Kodichath

Sreedev Kodichath, a passionate software engineer, an avid blogger & an eloquent orator is the author of DevTechnica.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *