Introduction to Linux Shell (Part 3): Writing Your First Shell Script

In the first two parts, we learned the basics of Linux and Shell, and mastered a large number of commonly used commands. Now, it’s time to organize these commands into scripts to achieve automation!

This article will guide you in writing your first Shell script, mastering the basic structure, execution methods, and syntax rules of scripts.

1. What is a Shell Script?

A Shell Script is a text file written with Shell commands, essentially a sequence of commands executed in order. You can write repetitive tasks into a script and execute them with a single command, greatly improving work efficiency.

💡 Analogy: Shell Script = Command-based “Macro Script” + Simple Programming Language.

2. The First Script: Hello, Shell!

1. Writing the Script File

Open the terminal and use your favorite editor (such as <span>vim</span>, <span>nano</span>, or <span>gedit</span>) to create a script:

nano hello.sh

Enter the following content:

#!/bin/bash
# This is my first Shell script
echo "Hello, Shell!"

Explanation:

  • <span>#!/bin/bash</span>: Shebang line, specifies that the script should be executed using the bash interpreter

  • <span>#</span> lines start with a comment

  • <span>echo</span>: outputs text

2. Granting Execution Permissions

chmod +x hello.sh

3. Executing the Script

./hello.sh

Output:

Hello, Shell!

Congratulations, you have successfully written and executed your first Shell script!

3. Basic Structure of a Shell Script

A standard Shell script typically includes:

#!/bin/bash             # Declare the interpreter
# Script description (author, purpose, etc.)

# Define variables
name="Shell"

# Output content
echo "Hello, $name"

4. Variables and Strings

1. Defining Variables

myname="Linux"

Note:

  • There should be no spaces around the equals sign

  • It is recommended to use lowercase letters or snake_case for variable names, such as:<span>user_name</span>

2. Using Variables

echo $myname
echo "Hello, $myname!"

3. String Concatenation

greeting="Hello"
who="World"
echo "$greeting, $who!"

5. Getting User Input

The script can also interact with the user:

#!/bin/bash
echo "Please enter your name:"
read username
echo "Hello, $username!"

Example run:

Please enter your name:
Zhang San
Hello, Zhang San!

6. Special Variables (Positional Parameters)

When the script receives external parameters, you can use <span>$1</span> and <span>$2</span> to represent the 1st and 2nd parameters:

#!/bin/bash
echo "Script name: $0"
echo "First parameter: $1"
echo "Second parameter: $2"

Run:

./args.sh apple banana

Output:

Script name: ./args.sh
First parameter: apple
Second parameter: banana

Common special variables:

Symbol Meaning
<span>$0</span> Script name
<span>$1</span>~<span>$9</span> 1st to 9th parameters
<span>$#</span> Number of parameters
<span>$@</span> All parameters (processed as a whole)
<span>$*</span> All parameters (processed as a single string)

7. Practice Suggestions

Here are some small tasks for you to practice:

✅ Practice Questions:

  1. Write a script that takes two numeric parameters and outputs their sum

  2. Write a script that outputs the current date, username, and current path

  3. Write a script that prompts the user for their name and then greets them

✅ Run tests from the command line:

bash yourscript.sh

or

./yourscript.sh

8. Summary

Content Description
<span>#!/bin/bash</span> Specifies the script uses bash
<span>chmod +x</span> Adds execution permissions to the script
<span>echo</span>, <span>read</span> Output and input
<span>$1</span><span>$@</span> Passed parameter variables
Comments Use <span>#</span>

The charm of Shell scripts lies in their ability to combine simple commands into powerful tools; you have taken the first step into script programming!

✅ Next Article Preview:

Introduction to Linux Shell (Part 4): Control Flow and Conditional Statements

We will learn how to use structures like <span>if/else</span>, <span>case</span>, and <span>for/while</span> to implement logical judgments and loops, giving scripts “intelligent” behavior.

Leave a Comment