Day 2 - Linux Basics & Commands (continued) Intro to Shell-scripting
Introduction to Shell Scripting
Shell scripting is a powerful tool for automating repetitive tasks, managing system operations, and enhancing productivity in Linux environments. It combines the efficiency of command-line tools with the logic of programming, allowing users to write scripts that execute commands sequentially or based on conditions. Whether you're a system administrator, DevOps engineer, or simply a Linux enthusiast, learning shell scripting can significantly streamline your workflows.
What is a Shell?
In Linux, the shell acts as a command-line interpreter that bridges the user and the operating system. Shells like Bash and Sh are widely used for scripting. The most popular shell is Bash (Bourne Again Shell), and it’s the default in many Linux distributions.
Understanding Basic Shell Scripting Syntax
Shell scripts are easy to write and execute, basically scripts are linux commands which are stored in a sequence in a text file with executable permissions.
Here, [#/bin/bash] is generally referred to as (shebang), which tells the system which interpreter to use, in the case (bash). The sharp sign (#) and the bang sign (!) thats why called is the shebang.
echo is used to print the enclosed text in the terminal.
Let’s create a basic script
- Open a terminal and use a text editor like nano to create a file:
- Add the following content to the file:
- Save and close the file. Make the script executable:
- Run your script:
Note:
If you don’t use shebang then the text file is executed via shell. We can check the available shells by using cat /etc/shells command.
Extra Knowledge
Scripts can be executed in different ways:
./script1.sh: In this (.) represents the pwd (present working directory) and automatically take the path where the script is located.
bash /home/ubuntu/aman-devops/script1.sh: in this we’ve given the complete path before executing the script.
Echo statement (built-in)
The echo statement is a built-in command in Bash used to display messages or output. It is widely used in shell scripts to print text or variables to the terminal. Here's a proper explanation of the echo statement with use cases, different flags, and formats.
credits: (referenced this echo exmaple from: https://learning-ocean.com/)
Output:
Variables and Data Type:
Shell scripts store data in variables without specifying a type. These variable can store different data types like integers, string and boolean. The shell enables us to create, assign, and delete variables.
User Defined Variable
Syntax:
[variable_name=value] … (no spaces around =)
To access the value of variable in the script use $variable_name.
Concatenating variables to perform a joint output using “${…}${…}” in echo.
Bash also supports arrays, which can be defined using parentheses ():
How not to assign variables in shell script, some examples.
Output:
(Variables are case sensitive, Name and name are different variables)
Extra Knowledge
Variables by default are treated as strings, but can also be used to perform arithemtic operations by using (( )):
Here is a suitable example, where we use variables (num1 and num2) to store value integer value and perform arithmetic calculation to give the sum.
Output:
If you have any doubts, regarding this concept feel free to write down a comment below.
System Defined Variable:
Predefined variables provided by the shell environment that store information about the system and the shell itself.
Commands like env, set, printenv can be used to list all the available specified environment variables, shell variables and functions.
Commonly Used System Variables
$HOME: Describes the current user’s home directory.
$PATH: Explains the directories searched for executable files.
$USER: Describes the logged-in user.
$SHELL: Provides information about the default shell in use.
$PWD: Current working directory.
$OSTYPE: Operating system type.
$UID: User ID of the current user.
Sample bash to demonstrate how system variables can be used to access and display essential system and user information.
Output:
After executing the script we get values related to all the system infomation, which is necessary for day to day operations.
Conclusion
Shell scripting is a crucial skill for anyone working in a Linux environment, offering flexibility and power to automate tasks, manage systems, and enhance productivity. By understanding the basic syntax, working with variables, and using system-defined variables, you can begin writing efficient and powerful shell scripts.
Next post will cover control flow statements, functions, read input, command line arguments etc. Please follow me and drop a comment if you liked the blog.
Thanks
Subscribe to my newsletter
Read articles from Aman Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aman Sharma
Aman Sharma
Passionate about simplifying the complexities of DevOps, I bring 1.5 years of hands-on experience in cloud infrastructure management. I excel at optimizing testing and staging environments for pre-production runs, automating workflows, and troubleshooting challenges across diverse cloud environments. My expertise includes infrastructure management, containerization with Docker, API-based CRM integration, and performance monitoring - ensuring stability and efficiency at every stage of the software lifecycle. Always eager to learn, collaborate, and innovate, I am committed to driving impactful change in the DevOps space.