How to write a simple bash script

Ralf lumby
3 min readJun 15, 2019

Who should read this article?

Beginners looking to learn more about bash scripting or just curious about learning programming.

Why should you be reading this article?

Bash scripts are very powerful and can be extremely useful when it comes to automating repetitive tasks(daily log data, pulling stock information, scheduled emergency backups), combo a chain of linux commands into one command(also can work with any programming language scripts that has terminal compatibility), and setting up web servers.

You’ll be able to use these scripts on any Linux environment including Ubuntu, Debian, Linux Mint, RedHat, Fedora, OpenSUSE, Slackware, Kali Linux and more to come.

It will also run on Apple’s Mac OS X, Oracle’s Solaris, IBM’s AIX, HP’s HP-UX, FreeBSD, NetBSD, and OpenBSD.

Personally I have used bash scripts to automate daily scheduled backups, chain linux commands with python scripts, and create a more efficient workflow(switching from a range of DNS, sending automated emails)

Outline:

  • Pre-req
  • Step 1: Open Terminal
  • Step 2: Step 2: Create the bash script in the current directory with vi(lightweight text editor)
  • Step 3: Get permission to run script hello_world
  • Step 4: Run the hello_world bash script
  • Conclusion
  • TLDR

Pre-req:

Unix based OS or MacOS
disclaimer: This guide is using MacOS High Sierra Version 10.13.6 (Users with basic linux knowledge should be able to follow the guide as well)

Step 1: Open Terminal

First, press +space to open file system finder on MacOS

Start typing terminal in the search field. Do so until the Terminal icon appears. Press Enter or click the Terminal icon

Step 2: Create the bash script in the current directory with vi(lightweight text editor)

Run the below input command in your terminal to open vi with our file name being hello_world:
vi hello_world

Copy and paste the code below into your vi text editor:

Input :wq and press enter to save the file.

Side note: The first line of most bash scripts#!/bin/bashconsist of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.

Step 3: Get permission to run script hello_world

Run the input below in your terminal to get permission to run your bash script:
chmod 0755 hello_world

Step 4: Run the hello_world bash script

Enter the input below in your terminal to run the bash script:
./hello_world

You should see this response in the terminal:
Hello World!

Congratulations on running you first successful bash script!!!

Conclusion:

You have learned how to create, give yourself permissions, and run a bash script

This is only your first step to becoming a bash scripting master and I hope this article is a stepping stone that points in you the right direction for your career!

TLDR:

How to open terminal with MacOS, create a bash file with vi, give permissions to run bash script, and running the bash script.

Cheers and thank you for reading!

Contact me for any questions as I will answer as soon as I am free!
Github: https://github.com/sudoxx2
My Profile/ Contact Info: http://petermoung.com

--

--