Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Shell Script Dictionary
  3. Beginners Guide: Overview, Features, and Learning Path

Beginners Guide: Overview, Features, and Learning Path

This page provides an overview of shell scripting and guides you through the order in which to learn each feature. It covers what shell scripts are, the relationship between bash, sh, and POSIX (Portable Operating System Interface — an international standard defining common Unix-like OS specifications), and the practical learning path.

What is a Shell Script

A shell script is a text file containing commands written for a shell (command interpreter). It takes the commands you would type one by one in a terminal and organizes them into a file so they can be run as a sequence of operations.

Shell scripts require no special compilation. Simply add execute permission to a script file and it runs directly.

chmod +x script.sh
./script.sh
# or
bash script.sh

sh / bash / zsh — Types of Shells

ShellDescription
sh (POSIX sh)A standard specification defining a minimal set of features. Works on virtually all Unix-like operating systems
bash (Bourne Again Shell)An extended version of sh. Widely used as the default shell on Linux. Has extensions like [[ ]] and arrays
zshDefault shell since macOS Catalina (2019). Has many features that are a superset of bash
dashA lightweight POSIX-compliant shell. Used as the default POSIX sh on Ubuntu

The first line of a script (the shebang line) specifies which shell to use for execution.

#!/bin/bash
# When running with bash

#!/bin/sh
# When running with POSIX sh (for portability across environments)

Use Cases Where Shell Scripts Excel

Use CaseExamples
File operation automationLog backups, deleting old files
Combining commandsPiping multiple tools together
Scheduled execution (cron)Nightly backups, hourly log rotation
Deploy scriptsThe sequence: git pull → build → restart
Environment setupAutomating server setup procedures

Basic Shell Script Syntax

Variables

When assigning to a variable, do not add spaces around =. Reference a variable by prefixing it with $.

#!/bin/bash
name="Ayanami Rei"
echo "Hello, $name"
echo "Hello, ${name}"  # Curly braces make the variable boundary explicit
Hello, Ayanami Rei
Hello, Ayanami Rei

Conditionals and Comparisons

The if statement evaluates conditions using [ ] (POSIX) or [[ ]] (bash extension).

#!/bin/bash
score=85

if [ $score -ge 80 ]; then
  echo "Pass"
elif [ $score -ge 60 ]; then
  echo "Borderline"
else
  echo "Fail"
fi
Pass

Loops

#!/bin/bash
# Loop over a list
for member in "Ayanami Rei" "Ikari Shinji" "Soryu Asuka"; do
  echo "Pilot: $member"
done

# Numeric loop (bash extension)
for i in {1..3}; do
  echo "Count: $i"
done
Pilot: Ayanami Rei
Pilot: Ikari Shinji
Pilot: Soryu Asuka
Count: 1
Count: 2
Count: 3

Pipes and Redirection

One of the strengths of shell scripts is the ability to connect multiple commands with a pipe (|).

#!/bin/bash
# Count lines in a file
cat /var/log/nginx/access.log | wc -l

# Extract only error lines and save to a file
grep "ERROR" app.log > errors.txt

# Write both stdout and stderr to a file
./build.sh > build.log 2>&1

Recommended Learning Order

The following is one suggested order for working through the pages in this dictionary.

Summary

Shell scripts = Commands for a shell organized into a file. Widely used for file automation, command chaining, and deployments.

Shebang line (first line)

  • #!/bin/bash — When using bash-specific features
  • #!/bin/sh — When you need portability across environments (POSIX-compliant)

Variable basics

  • Assignment: name=value (no spaces around =)
  • Reference: $name or ${name}
  • Command substitution: result=$(command)
  • Arithmetic: n=$((a + b))

Commonly used special variables

  • $0 — The script's own filename
  • $1 through $9 — Arguments passed to the script
  • $# — Number of arguments
  • $@ — List of all arguments
  • $? — Exit code of the last command
  • $$ — Current process ID

Commonly Used bash Extensions

  • [[ ]] — Supports regex matching with =~ and writing AND/OR directly
  • (( )) — C-style arithmetic expression evaluation
  • Arrays — arr=(a b c), ${arr[0]}
  • {1..10} — Brace expansion (sequence generation)

Common Mistakes

Adding spaces around the assignment operator

This is one of the most common mistakes in shell scripting.

name = "Ayanami Rei"  # Wrong: name is interpreted as a command name
name="Ayanami Rei"    # Correct

Using variables without quotes

If a variable contains spaces, omitting quotes can cause unintended word splitting.

name="Ayanami Rei"
echo $name    # Works, but word splitting occurs for values with spaces
echo "$name"  # Safe. Make it a habit to always wrap variables in double quotes

If you find any errors or copyright issues, please .