Fundamentals 18 min read

Master Shell Scripting: From Basics to Advanced Tricks and Performance Tips

This comprehensive guide explains what a shell is, compares common shells, walks through creating and running shell scripts, covers shebang usage, execution methods, variable types, special and environment variables, substring manipulation, built‑in commands, performance benchmarking, and provides practical examples for Linux system automation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Shell Scripting: From Basics to Advanced Tricks and Performance Tips

What Is a Shell?

A shell is a command‑line interpreter that sits between the user and the operating system, translating user input into actions and displaying output. It enables both interactive commands and script execution.

Common Shell Types

Bash (Bourne Again Shell) : Most widely used, default on many Linux distributions and macOS.

Sh (Bourne Shell) : The original Unix shell, simpler feature set.

Tcsh (C Shell) : C‑style syntax, convenient for C programmers.

Zsh (Z Shell) : Advanced features like auto‑completion and theming; default on macOS Catalina and later.

Fish (Friendly Interactive Shell) : Modern, user‑friendly with syntax highlighting.

What Is a Shell Script?

A shell script is a text file containing a sequence of shell commands and control structures. Scripts can be executed interactively (non‑interactive mode) or by invoking the interpreter directly.

# First script example (test1.sh)
#!/bin/bash

echo "hello world!"

Run with bash test1.sh or make the file executable and run ./test1.sh.

Shebang (#!)

The shebang is the first line #!/path/to/interpreter that tells the system which interpreter to use. If omitted, the script runs with the current shell ($SHELL). Errors occur if the interpreter path is missing, non‑executable, or nonexistent.

Executing Scripts

Explicit interpreter: bash script.sh or sh script.sh (no execute permission needed).

Direct execution: chmod +x script.sh then ./script.sh (requires a valid shebang).

Variables

Variables are defined without spaces: name="zfox". Bash treats all variables as strings (weak typing). Use ${var} for substitution.

System Variables

Common ones include HOME, PWD, SHELL, USER. List all with set or env.

Custom Variables

# Define
myvar="value"
# Read
echo $myvar
# Delete
unset myvar
# Read‑only
readonly myvar

Quote Differences

Single quotes: literal, no expansion.

Double quotes: allow variable and command substitution.

Backticks or $(...): command substitution.

Special Variables

$?

: Exit status of last command (0 = success). $#: Number of positional parameters. $* and $@: All arguments; quoted "$*" yields a single string, "$@" preserves each argument. $0$9: Script name and positional arguments.

Environment Variables

Exported variables define the shell’s runtime environment. They can be set temporarily in a session or permanently via ~/.bashrc, ~/.bash_profile, or global files under /etc/profile.d/. Use export VAR=value to make a variable available to child processes.

Managing Environment Variables

View: printenv, env, set.

Remove: unset VAR.

Read‑only: readonly VAR (effective until shell exits).

Substring Operations

Parameter expansion provides powerful substring handling:

# Assume name="yuchao180"
${#name}          # length (9)
${name:3}        # from offset 3 -> "hao180"
${name:2:4}      # offset 2, length 4 -> "chao"
${name#*c}       # remove shortest prefix matching *c
${name##*c}      # remove longest prefix matching *c
${name%*c}       # remove shortest suffix matching *c
${name%%*c}      # remove longest suffix matching *c
${name/o/O}      # replace first o with O
${name//o/O}     # replace all o with O

String Length Benchmarks

Comparing methods for measuring string length:

# Using built‑in ${#var}
# Using wc -L
# Using expr length "$var"
# Timing with time command shows ${#var} is fastest.

Built‑in Commands Overview

echo

: print text (options -n, -e for no newline and escape interpretation). eval: evaluate a string as a command. exec: replace the current shell with a new program (no fork). read, shift, export, declare etc.

Practical Examples

Creating a File via Script

#!/bin/bash
cd /home/zfox
touch ljw.txt
echo "I love ljw" > ljw.txt

Batch Renaming Files

# Remove "_finished" from all jpg files
for f in *finished.jpg; do
  mv "$f" "${f//_finished/}"
done

Loop Performance Test

time for i in {1..10000}; do
  str=$(seq -s "chaoge" 100)
  echo ${#str} >/dev/null
 done

Conclusion

Shell scripting remains the most efficient tool for Linux system automation, especially when combined with the powerful trio of grep, sed, and awk. Understanding variable scopes, quoting rules, and built‑in commands enables developers to write concise, high‑performance scripts for tasks ranging from simple file manipulation to complex system monitoring.

Shell execution flow diagram
Shell execution flow diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellScriptingBashVariablesEnvironment
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

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.