Linux Bash

Most professionals in the software development and IT industry need to learn how to use different tools for them to remain relevant. The professionals use Linux a lot, and knowing how to use some of the Linux tools like Bash is very useful as it helps you have more power over the entire operating system. Many people are wondering what Bash is and how it can help them in their daily activities. Most IT jobs require one to know how to use Bash to create scripts. The scripts enable users to enjoy some of the activities they struggle with. They make debugging easy and support all versions of the Linux operating system. You can run processes using multiple commands; they are portable, save time, e.t.c. Understanding Linux Bash greatly impacts how the team runs all their operations. The article will enable the readers to understand what bash is, how to write different Bash scripts and its installation, e.t.c.

What is Bash?

Bash is the acronym of Bourne-Again Shell, which got named after the famous Stephen Bourne, who used it as a bunter. It works as an interpreter for the different operating systems. It is compatible with other scripts like sh and works well with features from ksh and csh shells. Developers use it to look up different commands and search different directories to see if there are directories that match the typed command. Bash executes the commands if there are matching directories or what it was instructed to do. Most of the users enjoy how Bash has improved their productivity and performance.

What is Bash in Linux?

Bash is a type of shell that is Unix based and was mostly used as the default shell for the Linux distributions like Ubuntu, Kali, Fedora, e.t.c.It works as a command processor that usually works under a text window. It helps users execute and read different commands from different files. Using the globbing technique, you can use it to identify different characters from files. It also uses message passing to improve communication between different computer systems. It has stream literals that support different features of file formatting like line breaks and whitespaces. It uses different keywords, variables, and syntax to get the desired result. It also works well with other extensions to improve its functionalities.

Become a Linux Certified professional by learning this HKR Linux Training !

Linux Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

How to download Bash

There are several steps one has to follow when downloading Bash. These steps are:

Open the terminal and type the following command:

sudo apt-get install build-essential

After that process is complete, you can access the GNU servers to get some important packages using the following command:

wget https://ftp.gnu.org/gnu/bash/bash-4.4.tar.gz

After getting the packages, we need to compile the packages and install them by typing the command below:

tar xf bash-4.4.tar.gz

After that, head over to the created directory of bash by typing :

cd bash-4.4

After getting access to the directory, we need to carry out the configuration by typing the following command:

./configure

Complete the steps by typing the commands below:

make

sudo make install

Complete the installation by typing the command below, and you are good to use Bash.

sh

How To Write Bash Scripts in Linux

There are several ways of how one can write Bash scripts in Linux. Let us look at how you can write several scripts. When working with scripts, you will notice that it starts with #! Which shows where the scripts start. When you want to add comments to the scripts, you use # to add them.

How to Write Bash Script that Accepts user Input

If you want to collect some information using a script or make it interactive. You can make the script accept input from different users.

  • First, create a new file called studentrecords.sh and open it using nano as below:
nano studentrecords.sh
  • Ensure the interpreter of the program is set to Bash by having the shabeng code as below:
#!/bin/bash
  • If you want to add a message that the users can read, we use the echo command, i.e. echo "Welcome to the Klimack University"
  • We use the read -p command to take the keyboard inputs. Most of the inputs get saved as variables i.e
read -p "Provide us with your registration Number?: "registration_number

After inputting the name, we will provide the user with different options to choose which course they do the university. We will use an array list and assign each course to an index as shown below:

course[0]="Data science"

course[1]="Computer Science"

course[2]="Human Resource"

course[3]="Business Management"

course[4]="Education"

course[5]=” Engineering”
  • Now let us create a new variable called studentdetails which will help in storing the array items as below:
studentdetails=${#course[@]}
  • Let us create another variable called studentissue that will help in selecting random numbers from the array as below:
studentissue=$(($RANDOM % $studentdetails))
  • We now use the echo command to print any course name and the registration number of the student randomly using the following command
echo ${course[$studentissue]} $registration_number
  • You then press Ctrl + X to save the code, followed by Y and then hit Enter.
  • To test if the code is running from the terminal, we use the following command:
bash  studentrecords.sh

Want to know more about Linux,visit here Linux Tutorial !

Subscribe to our youtube channel to get new updates..!

How to write “Good morning” script in Linux

It will help us understand how to write basic bash scripts in Linux. This simple script will help us learn how to work with Bash.

  • First, we will create a new file called goodmorning.sh and then we will open it using the nano command as below:
nano goodmorning.sh
  • When you hit Enter, you will see a hashtag sign(#) at the beginning. It is normally referred to as a shebang and shows where the script will start.
#!/bin/bash
  • For us to print new text, we use the echo command as shown below:
echo “Good Morning”
  • To apply the changes, we Press Ctrl + X, followed by Y, then hit Enter.
  • To see if the code is running, type bash goodmorning.sh on the terminal and hit Enter. You will see the result that prints a line of text having Good Morning characters meaning the script worked.

How to write Bash Scripts using arguments

When you want to improve automation, you make a bash script that can help you achieve that. In cases where you find yourself typing the same things repeatedly, you can use Bash to eliminate the repetitive processes. 

  • First, let us create a new file that has the .sh extension, for example, records.sh
  • Add the normal shebang as  #!/bin/bash.
  • Use the chmod command together with +x arguments to make the file executable.
chmod +x records.sh
  • If you are running the script on a certain website, you will have to add the  ./ before the executable file as below:
./records.sh https://google.com
  • We now use the sudo command to access the /usr/bin/  directory since we lack permission and later copy the file to the directory and help the users remove the .sh extension.
  • sudo cp /records.sh /usr/bin/records
  • Go to the terminal, type records followed by the URL you want to check, and hit Enter.

How to write Bash Scripts using arguments

You must add different parameters to the commands when using the terminal to handle different arguments. For example, when y you want to check the status of a URL, we will follow the steps below:

  • First, we have to create a new file called checker.sh and open it using the nano command:
nano checker.sh

Let the interpreter understand by adding the shebang

#!/bin/bash

We now use the echo command to print the status code of the URL, and you will have to add the value that you will use under the argument i.e

echo "The HTTP code for URL " 3 " is..."
  • There are several arguments one can use to display the output. For example, we can use –write-out argument for getting the HTTP code and –silent to ensure we have little output on the screen.
  • To get the response code, we use the echo command to get it i.e
echo "${response}"
  • To apply the changes, we Press Ctrl + X, followed by Y, then hit Enter.
  • Go to the terminal and run the file as follows
bash checker.sh https://gumanlen.com

 Top 50 frequently asked Linux Interview Questions !

Linux Certification Training

Weekday / Weekend Batches

Features of Linux Bash

Linux Bash has different features. Some of the features include:

  • Shell Syntax. Its syntax supports various operations. Some of the syntaxes include the use of comments, quotes, e.t.c. When you get the syntax wrongly, it may end up not running or getting errors. When you use quotes, it enables users to get special meaning from the used characters.
  • Shell commands. It has many commands that one can choose from. Some commands are compound, simple, lists, pipelines, e.t.c. Each command has a different action. It also uses command substitution to allow the running of the commands and use its output as an argument when dealing with other commands.
  • Shell expansions. It involves the use of Bash in expanding different parameters. The expansion is successful when you split the input into different tokens when using it on the command line.
  • Redirections. They help in controlling the input and output of the operations. It allows users to collect messages and different inputs and direct them into different files. When you get an error, you won't see them on the monitor; instead, you can access them in a file.
  • Shell functions. The functions consist of several commands which get grouped to perform certain execution. They work like normal commands, and when you use the name of one of the functions as a command, all the commands under the group get executed.
  • Shell parameters. The parameters provide storage for the variables. The variables can be numbers, characters, values, names, e.t.c. There are two types of parameters, namely: special and variable. Special parameters are usually read-only and are normally preset by the shell, while variable parameters are managed by users and the shell and are the commonly used parameters. They provide more features to the commands by providing more options.
  • Command execution. It helps the computer system to run different commands.
  • Shell scripts. The shell script is a type of text file that uses different commands to run the program on the Shell using Bash. It usually has some comments that outline the steps one has to follow. Many IT professionals use scripts in processes like file manipulation, text printing, program execution, e.t.c.
Conclusion

Now you have an understanding of Linux Bash and how to use it. To master some of the essentials and use them with more proficiency, you need to do more practice or attend training on Linux. Those who have little or no programming experience can also enjoy using Bash. It is not complicated, as many other people put it. Many IT administrators must be good at using Linux Bash to handle tasks faster.

Related blogs:

Find our upcoming Linux Certification Training Online Classes

  • Batch starts on 5th Dec 2022, Weekday batch

  • Batch starts on 9th Dec 2022, Fast Track batch

  • Batch starts on 13th Dec 2022, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Saritha Reddy
Saritha Reddy
Research Analyst
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.

Bash automates most development tasks like debugging, testing, change management, code compilation, e.t.c.

No, Linux is not the same as bash.

It is found under the /usr/bin/ directory.

Linux mostly uses shell, but you can use bash in some cases.

Open your machine terminal and type, and then hit Enter. If it is not successful, you will get the message.