Operations 17 min read

Master Shell Scripting: A Hands‑On Guide to Bash Basics and Advanced Tricks

This comprehensive tutorial explains what a shell is, introduces common shell types, walks through creating and executing shell scripts, covers variables, quoting, special parameters, environment settings, and demonstrates practical string manipulation and batch‑renaming techniques for Linux users.

Raymond Ops
Raymond Ops
Raymond Ops
Master Shell Scripting: A Hands‑On Guide to Bash Basics and Advanced Tricks

What is a Shell

Shell is a command interpreter that wraps the OS kernel, allowing users to interact with the system by entering commands which are interpreted and the results displayed.

Common Shell Types

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

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

Tcsh (C Shell) : C‑like syntax.

Zsh (Z Shell) : advanced features like auto‑completion, default on macOS Catalina+.

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

What is a Shell Script

A file containing a sequence of shell commands and control structures. In Linux the typical extension is .sh; on Windows .bat. Shell scripts are weakly typed and interpreted.

Shell Script Rules

Scripts are usually edited with vim and consist of Linux commands, Bash directives, logic statements and comments. The first line often starts with a shebang.

First Shell Script

# vim test1.sh
#!/bin/bash

echo "hello world!"

Run with bash test1.sh to output hello world!.

Shebang

The #! line tells the system which interpreter to use, e.g., #!/bin/bash. If omitted, the current shell ($SHELL) is used.

Common Execution Methods

Run with bash script.sh or sh script.sh (no execute permission needed).

Make script executable ( chmod +x script.sh) and run ./script.sh or with absolute path.

Shell Variables

Variables are defined without spaces, e.g., name="zfox". They are weakly typed strings.

System Variables

Common ones include HOME, PWD, SHELL, USER. View with echo $HOME or set.

Custom Variables

Define with var=value, delete with unset var, make read‑only with readonly var.

Quoting

Single quotes preserve literal text, double quotes allow variable expansion, backticks or $(...) capture command output.

Special Variables

$?

– exit status. $# – number of arguments. $* and $@ – all arguments, with differences when quoted. $0, $1 … – script name and positional parameters.

Environment Variables

Exported with export VAR=value to affect child processes. Persistent definitions go in ~/.bashrc, ~/.bash_profile or /etc/profile.d/. List with env or printenv. Remove with unset VAR.

String Manipulation

Substring extraction: ${var:offset:length}. Length: ${#var}. Pattern removal: ${var#pattern}, ${var##pattern}, ${var%pattern}, ${var%%pattern}. Replacement: ${var/pattern/repl} or ${var//pattern/repl}.

Practical Example – Batch Rename

#!/bin/bash
for file in *finished.jpg; do
    mv "$file" "${file//_finished/}"
done

This script removes the “_finished” suffix from all matching JPG files.

Command LinebashVariablesshell scriptingenvironment variablesShebang
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.