DEV Community

Cover image for Bash Scripting: An Introduction
El-karece Asiedu
El-karece Asiedu

Posted on • Edited on

Bash Scripting: An Introduction

Bash stands for Bourne Again Shell because it is an improvement on the previously existing shell. This blog post is to understand the whole concept of bash and get why developers really use bash! This is just the beginning so it will cover the basics.

Bash is used on linux OS. I will be using Mac OS in this. blog post the default shell is zsh. To use bash installed some dependencies to get started.

Bash can be used for virtual machines, deploying services and automating basic tasks.

write a bash script with me

Open terminal and type the following commands to change your file directory and move from zsh to bash.

Image description

To access bash terminal, type these commands. Notice the change to bash.

Image description

Create a new file as .sh file for scripting.

Image description

Use "nano" to open the script in terminal and start typing bash script.

Image description

Every bash script should begin with this code below known as "Shebang".

!# /bin/bash
Enter fullscreen mode Exit fullscreen mode

Now type the bash script.

!# /bin/bash
echo "Good morning Karece!"
Enter fullscreen mode Exit fullscreen mode

Select Ctrl + x on your keyboard then select "y" also on your keyboard, then enter to save the script.

Check permissions. And make your script executable.

ls -al

Image description

Now run your bash script using.

Image description

That's the first bash script!

Another bash script you can try.

 #!/bin/bash

name=$1
compliment= $2

user=$(whoami)
whereami=$(pwd)
date=$(date)

echo "Good morning $name!!"
sleep 1
echo "You're looking good today $name!"
sleep 1
echo "You have the best $2 ever $name!"

sleep 2

echo "You are currently logged in as $user and you are in the directory 
$whereami. Also today is: $date"
Enter fullscreen mode Exit fullscreen mode

Run with

./nameofscript Karece Code
Enter fullscreen mode Exit fullscreen mode

Some helpful videos can be found here

Done!

Top comments (0)