Special Variables

These variables have special meaning in the shell.

Source: src/scripting/variables.f90, src/execution/executor.f90

Positional Parameters

VariableDescription
$0Script name or shell name
$1 - $9Positional parameters 1-9
${10}Positional parameter 10+ (braces required)
$#Number of positional parameters
$@All parameters as separate words
$*All parameters as single word

$@ vs $*

set -- "arg one" "arg two" "arg three"

# Quoted $@ preserves word boundaries
for arg in "$@"; do
    echo "Arg: $arg"
done
# Arg: arg one
# Arg: arg two
# Arg: arg three

# Quoted $* joins with first char of IFS
for arg in "$*"; do
    echo "Arg: $arg"
done
# Arg: arg one arg two arg three

Manipulating Parameters

shift                         # Remove $1, shift others down
shift 3                       # Remove $1-$3
set -- "new" "args"          # Replace all parameters

Status Variables

VariableDescription
$?Exit status of last command
$!PID of last background command
$$PID of current shell
$-Current shell options
$_Last argument of previous command

Exit Status $?

true
echo $?                       # 0

false
echo $?                       # 1

ls /nonexistent 2>/dev/null
echo $?                       # 2 (or similar)

Background PID $!

sleep 100 &
echo "Started sleep with PID $!"
kill $!

Current Shell $$

echo "This shell's PID: $$"
echo $$ > /var/run/myscript.pid

Shell Options $-

echo $-                       # e.g., "himBHs"
set -x
echo $-                       # Now includes 'x'

Shell Information

VariableDescription
BASH_VERSIONShell version string
FORTSH_VERSIONfortsh version
SHELLPath to current shell
SHLVLShell nesting level
PPIDParent process ID
UIDUser ID
EUIDEffective user ID
HOSTNAMESystem hostname
echo "Running fortsh $FORTSH_VERSION"
echo "Nested $SHLVL levels deep"
echo "User ID: $UID"

Path Variables

VariableDescription
PWDCurrent working directory
OLDPWDPrevious directory
HOMEHome directory
PATHCommand search path
CDPATHcd search path
echo "Current: $PWD"
echo "Previous: $OLDPWD"
cd -                          # Goes to $OLDPWD

Prompt Variables

VariableDescription
PS1Primary prompt
PS2Continuation prompt
PS3Select prompt
PS4Debug prompt (with set -x)

PS1 Escape Sequences

EscapeMeaning
\uUsername
\hHostname (short)
\HHostname (full)
\wCurrent directory
\WCurrent directory (basename)
\$# for root, $ for others
\tTime (HH:MM:SS)
\dDate
\nNewline
PS1='\u@\h:\w\$ '             # user@host:/path$

History Variables

VariableDescription
HISTFILEHistory file path
HISTSIZEMax history in memory
HISTFILESIZEMax history in file
HISTCONTROLHistory behavior
export HISTSIZE=10000
export HISTCONTROL=ignoredups:erasedups

IFS (Input Field Separator)

Controls word splitting:

IFS=$' \t\n'                  # Default: space, tab, newline

IFS=:
echo $PATH | tr ':' '\n'      # Split PATH

IFS=$'\n'
lines=($(cat file))           # Split on newlines only

Random and Sequences

VariableDescription
RANDOMRandom number 0-32767
LINENOCurrent line number
SECONDSSeconds since shell start
echo $RANDOM                  # e.g., 12345
echo "Line $LINENO"           # Current line
echo "Running for $SECONDS seconds"

Regex Captures

After [[ string =~ regex ]]:

VariableDescription
BASH_REMATCH[0]Full match
BASH_REMATCH[1]First capture group
BASH_REMATCH[n]Nth capture group
if [[ "v1.2.3" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
    echo "Major: ${BASH_REMATCH[1]}"   # 1
    echo "Minor: ${BASH_REMATCH[2]}"   # 2
    echo "Patch: ${BASH_REMATCH[3]}"   # 3
fi

Readonly Variables

These cannot be modified:

readonly PPID UID EUID HOSTNAME SHELLOPTS FORTSH_VERSION