Special Variables
These variables have special meaning in the shell.
Source: src/scripting/variables.f90, src/execution/executor.f90
Positional Parameters
| Variable | Description |
|---|
$0 | Script name or shell name |
$1 - $9 | Positional 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"
for arg in "$@"; do
echo "Arg: $arg"
done
for arg in "$*"; do
echo "Arg: $arg"
done
Manipulating Parameters
shift
shift 3
set -- "new" "args"
Status Variables
| Variable | Description |
|---|
$? | 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 $?
false
echo $?
ls /nonexistent 2>/dev/null
echo $?
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 $-
set -x
echo $-
Shell Information
| Variable | Description |
|---|
BASH_VERSION | Shell version string |
FORTSH_VERSION | fortsh version |
SHELL | Path to current shell |
SHLVL | Shell nesting level |
PPID | Parent process ID |
UID | User ID |
EUID | Effective user ID |
HOSTNAME | System hostname |
echo "Running fortsh $FORTSH_VERSION"
echo "Nested $SHLVL levels deep"
echo "User ID: $UID"
Path Variables
| Variable | Description |
|---|
PWD | Current working directory |
OLDPWD | Previous directory |
HOME | Home directory |
PATH | Command search path |
CDPATH | cd search path |
echo "Current: $PWD"
echo "Previous: $OLDPWD"
cd -
Prompt Variables
| Variable | Description |
|---|
PS1 | Primary prompt |
PS2 | Continuation prompt |
PS3 | Select prompt |
PS4 | Debug prompt (with set -x) |
PS1 Escape Sequences
| Escape | Meaning |
|---|
\u | Username |
\h | Hostname (short) |
\H | Hostname (full) |
\w | Current directory |
\W | Current directory (basename) |
\$ | # for root, $ for others |
\t | Time (HH:MM:SS) |
\d | Date |
\n | Newline |
PS1='\u@\h:\w\$ '
History Variables
| Variable | Description |
|---|
HISTFILE | History file path |
HISTSIZE | Max history in memory |
HISTFILESIZE | Max history in file |
HISTCONTROL | History behavior |
export HISTSIZE=10000
export HISTCONTROL=ignoredups:erasedups
IFS (Input Field Separator)
Controls word splitting:
IFS=$' \t\n'
IFS=:
echo $PATH | tr ':' '\n'
IFS=$'\n'
lines=($(cat file))
Random and Sequences
| Variable | Description |
|---|
RANDOM | Random number 0-32767 |
LINENO | Current line number |
SECONDS | Seconds since shell start |
echo $RANDOM
echo "Line $LINENO"
echo "Running for $SECONDS seconds"
Regex Captures
After [[ string =~ regex ]]:
| Variable | Description |
|---|
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]}"
echo "Minor: ${BASH_REMATCH[2]}"
echo "Patch: ${BASH_REMATCH[3]}"
fi
Readonly Variables
These cannot be modified:
readonly PPID UID EUID HOSTNAME SHELLOPTS FORTSH_VERSION