Fundamentals 7 min read

A Step‑by‑Step Tutorial for Writing Basic Shell Scripts on Linux

This article walks beginners through creating, editing, granting permissions to, and executing simple Bash scripts such as hello.sh and name_check.sh, while also covering common pitfalls, directory organization, and essential command‑line tools for effective shell scripting.

IT Xianyu
IT Xianyu
IT Xianyu
A Step‑by‑Step Tutorial for Writing Basic Shell Scripts on Linux

Many newcomers wonder where to write shell scripts; this guide starts from the very basics and shows you how to create, edit, and run scripts on a local machine, virtual machine, or cloud server using a terminal or SSH.

Recommended environment

Operating system: any Linux/Unix system (physical, VM, or cloud).

Login method: direct terminal access or ssh for remote servers.

Editor: vim (recommended) or nano for beginners.

Creating the first script: hello.sh

Step 1 – Choose a directory

Store all scripts under ~/scripts for easy management.

mkdir -p ~/scripts
cd ~/scripts

Step 2 – Create the file

Open the file with vim hello.sh . Press i to enter insert mode and paste the following content:

#!/bin/bash

echo "你好,Shell 脚本!"

Step 3 – Save and exit

Press Esc .

Type :wq and press Enter .

File saved successfully!

Step 4 – Make the script executable

Run chmod +x hello.sh to grant execution permission.

chmod +x hello.sh

Step 5 – Execute the script

./hello.sh

Output:

你好, Shell 脚本!

Common error troubleshooting

Error

Cause

Solution

command not found

Missing

./

before script name.

Run

./hello.sh

.

Permission denied

No execute permission.

Run

chmod +x hello.sh

.

No such file or directory

Wrong filename or path.

Use

ls

to verify location.

Second example: name_check.sh (user input and conditional)

Step 1 – Create the file

cd ~/scripts
vim name_check.sh

Enter insert mode ( i ) and paste:

#!/bin/bash

read -p "请输入你的名字:" name

if [ "$name" == "root" ]; then
    echo "欢迎管理员大人!"
else
    echo "你好,$name"
fi

Step 2 – Save, exit, and add permission

:wq
chmod +x name_check.sh

Step 3 – Run and test

./name_check.sh

When you type jack you see 你好,jack ; typing root prints 欢迎管理员大人! .

This demonstrates variables, user input, and conditional statements in Bash.

Organizing scripts

Keep scripts in a structured directory, for example:

~/scripts/
├── base/        # beginner scripts
├── monitor/     # monitoring scripts
├── backup/      # backup scripts
└── deploy/      # deployment scripts

Move your scripts into the appropriate folder:

mkdir -p ~/scripts/base
mv hello.sh name_check.sh ~/scripts/base/

Final checklist

Create script files with vim and save using :wq .

Grant execution rights with chmod +x .

Use variables, conditionals, and user input.

Store scripts in a well‑organized directory hierarchy.

These steps form the foundation for writing effective shell scripts before moving on to cron jobs, functions, or logging.

Linuxcommand linetutorialbashPermissionsshell scripting
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.