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. [Setup] Shell Script Environment

[Setup] Shell Script Environment

A shell script is a text file containing commands that a shell (command interpreter) can interpret and execute. By writing the operations you perform manually in a Linux or macOS terminal into a file, you can easily automate repetitive tasks and ensure reproducible server configurations. Common shells include bash (Bourne Again SHell), sh (Bourne Shell), and zsh (Z Shell). Some syntax and features differ depending on which shell you use.

Syntax

# -----------------------------------------------
#  Basic shell script syntax
# -----------------------------------------------

# Line 1: shebang
#   → Specifies the path of the interpreter used to run the script.
#   → Must be written on the very first line of the file (no blank lines before it).
#!/bin/bash

# -----------------------------------------------
#  Common shebang styles
# -----------------------------------------------

#!/bin/bash
#   → Explicitly specifies bash. Bash-specific extended syntax is available.
#   → The most widely used style.

#!/bin/sh
#   → Uses a POSIX-compliant sh.
#   → Suitable when portability is a priority, or for environments where bash
#     is not installed, such as Alpine Linux.

#!/usr/bin/env bash
#   → Searches PATH for the installed bash and runs it.
#   → Useful on macOS where bash may be installed under /usr/local/bin.

#!/bin/zsh
#   → Uses zsh. The default shell on macOS.
#   → Includes many convenient features for interactive use.

# -----------------------------------------------
#  Granting execute permission
# -----------------------------------------------

# chmod +x {script-file}
#   → Grants execute permission to the script.
#   → Only needs to be run once when the file is first created.
# chmod +x script.sh

Execution Methods

MethodSyntaxDescription
Direct execution (path)./script.shRequires execute permission (chmod +x). Runs using the shell specified in the shebang.
Explicit shell executionbash script.shWorks without execute permission. You can also use sh or zsh instead of bash.
Run in current shell (source)source script.shRuns the script within the current shell process. Variables defined in the script are carried over to the current shell.
Run in current shell (dot). script.shShorthand for source. POSIX-compliant syntax that also works with sh.
Run on bash startupbash -c "command"Runs a command string directly without creating a file. Used in Docker CMD instructions and CI/CD inline scripts.

Sample Code

greet_evangelion.sh
#!/bin/bash
# -----------------------------------------------
#  Script that greets five Evangelion pilots
# -----------------------------------------------

# Display the script name and current date/time
echo "=== Script started: $(date '+%Y-%m-%d %H:%M:%S') ==="
echo ""

# Store pilot names in an array
pilots=("Shinji Ikari" "Rei Ayanami" "Asuka Langley Soryu" "Kaworu Nagisa" "Mari Makinami Illustrious")

# Greet each pilot with a for loop
for pilot in "${pilots[@]}"; do
    echo "Hello, ${pilot}!"
done

echo ""
echo "=== Script finished ==="
$ chmod +x greet_evangelion.sh
$ ./greet_evangelion.sh
=== Script started: 2026-03-27 10:00:00 ===

Hello, Shinji Ikari!
Hello, Rei Ayanami!
Hello, Asuka Langley Soryu!
Hello, Kaworu Nagisa!
Hello, Mari Makinami Illustrious!

=== Script finished ===
Checking the difference with source
#!/bin/bash
# -----------------------------------------------
#  Demonstrates the difference between source and bash
#  (Pay attention to variable scope.)
# -----------------------------------------------

# Set the pilot name in the variable PILOT
PILOT="Shinji Ikari"

echo "PILOT inside script: ${PILOT}"
# --- Running with bash ---
$ bash set_pilot.sh
PILOT inside script: Shinji Ikari
$ echo $PILOT
                        ← The variable is not carried over to the current shell.

# --- Running with source ---
$ source set_pilot.sh
PILOT inside script: Shinji Ikari
$ echo $PILOT
Shinji Ikari           ← With source, the variable remains in the current shell.

Differences Between bash, sh, and zsh

ShellCharacteristicsCommon Use Cases
bashThe standard shell on Linux. Supports many extended features including arrays, [[ conditional expressions, and process substitution.Well-suited for general scripting on Linux servers.
shSupports only the minimal POSIX (Portable Operating System Interface — an international standard defining common specifications for Unix-like OSes) syntax. Bash extensions such as arrays are not available.Well-suited for environments without bash such as Docker or Alpine Linux, and for scripts that prioritize portability.
zshThe default shell on macOS Catalina and later. Compatible with bash syntax and adds enhanced tab completion and array operations.Well-suited for scripting and interactive use on macOS.

Summary

A shell script is a mechanism for writing commands in a file and executing them automatically. The file begins with a shebang such as #!/bin/bash to specify the interpreter. The basic workflow is to grant execute permission with chmod +x and then run the script with ./script.sh. To run without granting permission, specify the shell explicitly with bash script.sh. Running with source script.sh (or . script.sh) executes the script within the current shell process, so variables and functions defined in the script are carried over to the calling shell. This is why source is used to reload .bashrc or .bash_profile. For details on file permission settings, see chmod / chown.

If you find any errors or copyright issues, please .