A Beginner's Guide to Linux Shell Scripting Commands

A Beginner's Guide to Linux Shell Scripting Commands

Introduction:

Linux shell scripting is a powerful tool that allows you to automate tasks, process text, and manipulate files and directories. In this beginner's guide, we'll go over some of the most commonly used shell scripting commands and provide examples of how they can be used

Echo Command :

The echo command is used to print messages to the terminal. It can be used to print a single line or multiple lines of text. For example, to print the message "Hello World" to the terminal, you would use the following command :


echo "Hello World"        

You can also print multiple lines of text by using the echo command with the "-e" option and using the newline character "\n" to separate the lines. For example, to print the following message to the terminal :

echo -e "Welcome to my blog!\nThis is a beginner's guide to Linux shell scripting commands"        

This command help you to print


Welcome to my blog!
This is a beginner's guide to Linux shell scripting commands        

Variables :

Variables are used to store values that can be used later in a shell script. To create a variable, you simply give it a name and assign it a value. For example, to create a variable called "name" with the value "John", you would use the following command :


name = "John"        

You can then use the variable in your script by enclosing it in curly braces like so:


echo "My name is ${name}."        

This would print the message "My name is John." to the terminal.

if Statement :

The if statement is used to perform conditional branching in a shell script. It allows you to execute a block of code if a certain condition is true. For example, to check if a variable called "age" is greater than 18 and print a message if it is, you would use the following code :


if [ $age -gt 18 ]
then
  echo "You are an adult."
fi

        

In this example, the code inside the "if" statement will only be executed if the value of "age" is greater than 18.


for Loop :

The for loop is used to iterate over a list of items and perform a certain action for each item in the list. For example, to print the numbers 1 to 5 to the terminal, you would use the following code :


for i in 1 2 3 4 5
do
  echo $i
done

        

In this example, the code inside the "do" block will be executed once for each item in the list.


Conclusion:

Linux shell scripting commands can be a powerful tool for automating tasks and manipulating files and directories. In this beginner's guide, we covered some of the most commonly used shell scripting commands, including the echo command, variables, if statements, and for loops. With these commands in your toolkit, you'll be well on your way to becoming a proficient Linux shell scripter.

To view or add a comment, sign in

More articles by Rohith Namala

Explore content categories