Linux sh Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn the basics of shell scripting in Linux, including understanding shell variables and parameters, as well as implementing conditional statements and loops. You will create and run various shell scripts to automate tasks and streamline your workflow. The lab covers topics such as creating and executing shell scripts, working with shell variables, and using command-line arguments. The content is practical and hands-on, providing you with the necessary skills to effectively utilize the Linux shell for scripting and programming purposes.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") subgraph Lab Skills linux/echo -.-> lab-422915{{"`Linux sh Command with Practical Examples`"}} linux/env -.-> lab-422915{{"`Linux sh Command with Practical Examples`"}} linux/chmod -.-> lab-422915{{"`Linux sh Command with Practical Examples`"}} linux/nano -.-> lab-422915{{"`Linux sh Command with Practical Examples`"}} linux/export -.-> lab-422915{{"`Linux sh Command with Practical Examples`"}} end

Understand the Basics of Shell Scripting

In this step, you will learn the basics of shell scripting in Linux. Shell scripts are small programs written in the shell language that can automate various tasks and streamline your workflow.

First, let's create a new shell script file using the nano text editor:

cd ~/project
nano hello.sh

In the file, add the following content:

#!/bin/bash
echo "Hello, World!"

The first line #!/bin/bash is called the "shebang" and tells the system to use the Bash shell to execute the script.

To make the script executable, run:

chmod +x hello.sh

Now, you can run the script using:

./hello.sh

Example output:

Hello, World!

The echo command is used to print the "Hello, World!" message to the console.

Next, let's explore some basic shell variables. Create a new script called variables.sh:

nano variables.sh

Add the following content:

#!/bin/bash
NAME="John Doe"
echo "My name is $NAME"

Run the script:

chmod +x variables.sh
./variables.sh

Example output:

My name is John Doe

In this example, we defined a variable NAME and used it in the echo command.

Utilize Shell Variables and Parameters

In this step, you will learn how to work with shell variables and parameters in your scripts.

First, let's create a new script called args.sh that demonstrates how to use command-line arguments:

cd ~/project
nano args.sh

Add the following content:

#!/bin/bash
echo "Positional parameter 1: $1"
echo "Positional parameter 2: $2"
echo "All parameters: $@"
echo "Number of parameters: $#"

Save the file and make it executable:

chmod +x args.sh

Now, run the script with some arguments:

./args.sh apple banana cherry

Example output:

Positional parameter 1: apple
Positional parameter 2: banana
All parameters: apple banana cherry
Number of parameters: 3

In this example, we accessed the command-line arguments using the special variables $1, $2, $@, and $#.

Next, let's create a script that demonstrates the use of environment variables:

nano env_vars.sh

Add the following content:

#!/bin/bash
echo "User's home directory: $HOME"
echo "Current working directory: $PWD"
echo "User's shell: $SHELL"

Save the file, make it executable, and run it:

chmod +x env_vars.sh
./env_vars.sh

Example output:

User's home directory: /home/labex
Current working directory: /home/labex/project
User's shell: /bin/bash

In this example, we used the predefined environment variables $HOME, $PWD, and $SHELL.

Implement Conditional Statements and Loops

In this step, you will learn how to use conditional statements and loops in your shell scripts.

First, let's create a script that demonstrates the use of if-else statements:

cd ~/project
nano if_else.sh

Add the following content:

#!/bin/bash
NUM=10
if [ $NUM -gt 0 ]; then
  echo "The number is positive."
else
  echo "The number is non-positive."
fi

Save the file, make it executable, and run it:

chmod +x if_else.sh
./if_else.sh

Example output:

The number is positive.

In this example, we used the if-else statement to check if the value of the NUM variable is greater than 0.

Next, let's create a script that demonstrates the use of for loops:

nano for_loop.sh

Add the following content:

#!/bin/bash
for i in 1 2 3 4 5; do
  echo "Iteration $i"
done

Save the file, make it executable, and run it:

chmod +x for_loop.sh
./for_loop.sh

Example output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

In this example, we used a for loop to iterate over a list of numbers from 1 to 5.

Summary

In this lab, you first learned the basics of shell scripting in Linux, including creating a simple "Hello, World!" script and working with shell variables. You then explored how to utilize shell variables and parameters, such as using command-line arguments in your scripts. These foundational skills provide a solid understanding of the shell scripting environment and enable you to automate various tasks and streamline your workflow.

Next, you will learn how to implement conditional statements and loops in your shell scripts, allowing you to create more complex and dynamic scripts that can adapt to different scenarios and requirements.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like