Bash special variables

Rohit Talukdar
1 min readMay 16, 2024

--

‘Cos I keep forgetting them and I need a place where I can remember them

$? : exit code of last foreground cmd
to interpret the exit codes look here: Exit Codes With Special Meanings (tldp.org) and documentation — How do I get the list of exit codes (and/or return codes) and meaning for a command/utility? — Unix & Linux Stack Exchange

$! : PID of most recent bg job

$- : current option flags as specified upon pgm invocation

$# : the number of positional params

$0: name of shell script

$* : all positional params, starting from one.

$@ : also all positional params, starting from one. subtle difference between the two, which am not going to explain here.

Significant bash exit codes:

TLDR: user defined exit codes can be from 80–113, but to futureproof will suggest 100–113, that gives 13 diff exit codes.

‘1’: Catch-all for general errors

‘2’: Misuse of shell builtins (according to Bash documentation)

‘126’: Command invoked cannot execute

‘127’: “command not found”

‘128’: Invalid argument to exit

‘128+n’: Fatal error signal “n” eg, 137=128+9(SIGKILL) for OOM

‘130’: Script terminated by Ctrl + C

‘255’: Exit status out of range

Ref:

  1. Special Parameters (Bash Reference Manual) (gnu.org)
  2. signal(7) — Linux manual page (man7.org)

--

--

Rohit Talukdar
Rohit Talukdar

Written by Rohit Talukdar

Just another guy striving to say hello to the world.

No responses yet