Skip to main content

Command Palette

Search for a command to run...

Shell Scripting Fundamentals

Published
3 min read
Shell Scripting Fundamentals
A

High functional, goal-oriented, and result driven Analyst with 2+ years of progressive experience. contributing to organizational success through high-level strategy and the implementation of excellence. Strong track record of inspiring effective cross-functional collaboration and innovative problem solving skills.

Passionate about exploring the ever-evolving world of DevOps technology. On a journey to unlock the secrets of seamless development, continuous integration, and deployment.

Committed to continuous improvement and lifelong learning.

📝Understanding Linux Shell

The Linux Shell, or simply "SHELL" serves as the middleman between the user and the Linux kernel, allowing users to communicate with the operating system and to utilize its resources.

The Linux shell's primary function is that of a command interpreter, connecting users and the Linux kernel. It serves as the interface for sending and receiving commands to and from the operating system. However, its function goes much beyond simple translation; it provides a setting in which you have unmatched control over your system.

Just like any OS, or application Shell also has many types which are Bash (Bourne Again Shell), Zsh (Z Shell), Dash (Debian Almquist Shell)..etc.

In this blog, we'll be exploring more about BASH shell scripting.

📜Getting started with Shell Scripting

You must be wondering why BASH scripting. Bash is well-known for its extensive feature set, scripting capabilities, and widespread use. Many Linux distributions, including Ubuntu and CentOS, use it as the default shell. Because of its feature set, it has been extensively used by both beginners and experienced users as well.

The scripting files have an extension of .sh. Just like any other programming and scripting language Bash has a syntax that includes parameters, comments, and subcommands that the shell must follow.

📍Writing the Shell Script

The script file which has an extension of .sh should always start with shebang or sha-bang i.e. #! This is crucial because it instructs the kernel to use a specific interpreter to execute the commands found in the file.

For Bash Script, we write shebang as #!/bin/bash. This lets the kernel know that we would be running the bash script files.

Here's an example of a shell script:

We can write another command in the script file. eg: ls, pwd, mkdir..etc

If we execute the above .sh file we'll get the following output:

📍Executing the Shell Script

To execute the bash shell script we use the ./filename.sh command. A newly written script does not have to execute permissions by default, therefore attempting to run it will result in an error as shown below. To run a shell script, we must first change its permissions with the chmod command (shown below). After changing the permissions, the script is ready to run.

📍Variable in the Shell script

Variables are used to store and manipulate data. They play a crucial role in storing, retrieving, and processing information, making them an essential concept in Shell scripting. This allows us to write more dynamic and adaptable scripts.

Check out this instance of a shell script using a variable.

Check out this instance of a shell script using a variable to take input from the user:

📍Conditional statements in the Shell script

You can modify or control the flow of your script based on specific criteria using conditional statements. The appropriate piece of code is performed when a particular condition is met. As a result, decision-making procedures may be implemented and different case types can be handled within the code. The most common conditional statements in bash are if, if-else, if-else-fi..etc.

Check out how the conditional statement is used in this shell script:

Expressions that can be used with conditional statements are of following type

📖Conclusion

Shell Scripting is considered one of the powerful tools to automate your manual daily repetitive tasks. It gives you the ability to automate tasks which fulfills one of the characteristics of the DevOps methodology. It gives you more control over the system.

You may unlock the full potential of Linux administration and streamline your daily work by comprehending the fundamentals of shell commands, scripting strategies, and control structures as you proceed with your scripting journey.