The Startup
Published in

The Startup

Writing your first shell/bash script

Simple bash script

I am a huge fan of automation, I don’t like wasting time on redundant tasks. There is a quote that has stuck with me from the first time I heard it but I do not know the author of the quote.

Time is like a river. You cannot touch the same water twice, because the flow that has passed will never pass again. Enjoy every moment of your life.

-Unknown

Hence, we should always strive to save as much time as possible, once you lose it, it’s gone and cannot be recovered. This is what made me get interested in scripting. Being able to write code that runs a redundant task.

I am going to outline some simple steps that will help you write your first shell script.

Shell and bash

Shell is a command-line interpreter and it is what is used to communicate with the Linux operating system. When commands are run on the Linux terminal, it is the shell that executes them.

There are various types of shells in Linux and bash is one of them. Other shells are the Bourne shell (sh), Korn shell (sh), and the C shell (csh).

Creating a directory for scripts and adding it to the PATH variable

$PATH variable is one of the default environment variables in Linux. It used by the shell to find executable files or commands. Once you add a directory to the path, you can run a script in that directory from anywhere in your system.

For me, my practice is to put all my scripts in a directory named ‘cmd’ under my home directory and then I add it to the path variable.

To create the cmd directory, you can either create a new folder or use the terminal. To use the terminal, run the command below in your home directory:

# to go to home directory
cd ~
# to create the cmd directory
mkdir cmd

To add the cmd directory in the path, open the hidden folder .bashrc, which is located in the home directory, and add the necessary line of code.

You can use any editor to edit .bashrc be it gedit, vi, or something else. You can google the command to run your favorite editor.

Run the command below to open the file.

# open .bashrc using gedit
gedit .bashrc
# open .bashrc using vi
vi .bashrc

The code to add the cmd directory to the path is:

export PATH="/home/username/cmd:$PATH"

You are now set to create your first script.

Writing your first script

Creating your script and changing permissions

For now, I am going to create a simple script that will just output the name of the user that is currently logged in and greet them. I use vi for this example but feel free to use any editor you want.

I ensure all created scripts are in the cmd directory as they’ve been added to the path. To create my script, I run the command below:

vi cmd/hello_user

When you create the script using vi, the system does not assign execute permission to any user group hence you will not be able to execute the script. To change the permission, the chmod command is run. If you want to know what chmod does, you can man it as shown below:

man chmod

There are 2 ways to change permissions, you can do it inside vi or directly from the terminal, the 2 options are shown below:

# run inside vi
:! chmod 755 %
# outside vi
chmod 755 cmd/hello_user

To run the command in vi, ensure you’re not in edit mode as commands can only be run in command mode. To exit edit mode, press esc key.

The exclamation mark (!) tells the shell to execute the command, and % denotes the current file.

Writing code in the script

First, we start the script by writing a shebang or a “bang” line. The shebang is used to tell the operating system which interpreter to use to parse the rest of the file.

#!/bin/bash

To run a command inside a script, use backticks ``. E.g to get currently logged in user, run the command below:

`logname`

You can man the logname command to know what it does.

man logname

Copy the code below to greet the user and display the name of the currently logged-in user:

Greet user and displayed name of the current user

Running the script

To run the script, write its name on the terminal and press enter. A script is a command that you run and execute some logic.

Running the script

Conclusion

I hope you enjoyed writing your first bash script. This is just the beginning of your journey in bash scripting. I will be sharing more articles to help you learn more advanced topics too. Feel free to ask any questions or to seek clarification if anything is not clear.

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

State of Serverless Cloud Computing

What was interesting at Dockercon

Emulators

SkyVerse Mint Updates

Fibonacci Functional Programming — A Walkthrough

📌 In this task, you have to create a Web Application for Docker (one of the great Containerization…

Get Real-Time Propane Prices With This API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sheila Mbadi

Sheila Mbadi

A software engineer and data scientist who is committed to creating high-quality products that solve the world's most pressing problems.

More from Medium

Linux Shell Programming: Creating Your First Shell Script

PowerShell — Linux-like history command

I found the best text editor for basic code fixing

Finding Commands by OutputType in PowerShell