Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quick Reference

A consolidated reference for hash shell features.

Command Line Options

OptionDescription
-c stringExecute commands from string
-iForce interactive mode
-l, --loginRun as login shell
-sRead from stdin
-a -b -C -e -f -m -n -u -xShell options, as for set
-v, --versionPrint version
-h, --helpShow help

Key Bindings

KeyAction
Left/RightMove cursor
Up/DownHistory navigation
Ctrl+ABeginning of line
Ctrl+EEnd of line
Home/EndBeginning/End of line

Editing

KeyAction
BackspaceDelete before cursor
DeleteDelete at cursor
Ctrl+UDelete to beginning
Ctrl+KDelete to end
Ctrl+WDelete word
Ctrl+HSame as Backspace

Other

KeyAction
TabAuto-complete
Ctrl+LClear screen
Ctrl+CCancel line
Ctrl+DEOF / Exit
Enter, Ctrl+JRun the line

set -o vi switches to vi mode; set -o emacs is the default.

Operators

OperatorDescription
|Pipe stdout to stdin
>Output to file (overwrite)
>>Output to file (append)
<Input from file
2>Stderr to file
&>Both stdout and stderr
2>&1Stderr to stdout
&&Run if previous succeeded
||Run if previous failed
;Run sequentially
&Run in background

Special Variables

VariableDescription
$?Exit code of last command
$$Shell process ID
$0Shell/script name
$1-$9Positional arguments
$#Number of arguments
$@All arguments (separate)
$*All arguments (single)
$-Option letters in effect
$LINENOCurrent line number

Test Operators

File Tests

TestDescription
-eExists
-fRegular file
-dDirectory
-rReadable
-wWritable
-xExecutable
-sSize > 0
-LSymbolic link

String Tests

TestDescription
-zEmpty string
-nNon-empty string
=Equal
!=Not equal

Numeric Tests

TestDescription
-eqEqual
-neNot equal
-ltLess than
-leLess or equal
-gtGreater than
-geGreater or equal

PS1 Escape Sequences

set prompt=NAME picks a preset: classic, plain, minimal, two-line, powerline; set prompt=list shows them. PS1 builds your own (Prompt Customization):

SequenceDescription
\wFull path
\WCurrent directory
\uUsername
\hHostname, up to the first dot
\HFull host name
\gGit branch + status
\bGit branch alone, nothing outside a repository
\eExit code color, between \e...\e
\?Last exit status when it was not 0
\$$ or # for root
\t \T \@Time: 24-hour, 12-hour, 12-hour with am/pm
\dDate
\jNumber of background jobs
\!History number of the next command
\s \vShell name and version
\nNewline
\\A backslash
\[ \]Ignored (bash’s colour-code brackets)

History Expansion

PatternDescription
!!Last command
!nCommand number n
!-nn commands ago
!prefixLast starting with prefix

Environment Variables

VariableDescription
HISTSIZECommands in memory
HISTFILESIZECommands in file
HISTFILEHistory file location
FUNCNESTFunction nesting limit, eval and . included (default 1000; 0 keeps only the stack guard)
HISTCONTROLHistory behavior
PS1Prompt format
PS2Continuation prompt
PS4Prefix of set -x lines
PATHCommand search path
HOMEHome directory
EDITORDefault editor

Shell Options

Set with set -x, clear with set +x; cluster letters (set -ex) or use the name (set -o xtrace). set -o alone lists them, set +o prints them as commands, and $- holds the letters in effect.

LetterNameEffect
-aallexportEvery assignment is exported
-bnotifyAccepted; background job status is reported at the prompt
-Cnoclobber> does not overwrite an existing file (`>
-eerrexitExit when a command fails
-fnoglobNo pathname expansion
-hAccepted; commands are remembered as they run
-mmonitorJob control
-nnoexecRead and parse commands without running them (not at the prompt)
-unounsetExpanding an unset variable is an error
-vverbosePrint input lines as they are read
-xxtracePrint each command, after expansion, before it runs (PS4 is the prefix)
nologKeep commands out of the history
nonlexicalctrlbreak and continue reach loops in calling functions
emacsEmacs-style line editing (the default)
viVi-style modal line editing; set -o vi turns emacs off and back

hash’s own set name=value options (prompt, colors, welcome, PS1, color.<element>, syntax_highlight, autosuggest, danger_highlight) are listed on the configuration page.

Built-in Commands

The Built-in Commands page has the full entry for each, the text help NAME prints; this table is the one-line form. Every builtin takes -- as the end of its options.

CommandSynopsis and meaning
:: [arg...]: do nothing, status 0
., source. file, source file: run a file in the current shell
[, test[ expr ], test expr: evaluate an expression (Test Command)
[[[[ expr ]]: extended test, with ==/!= patterns, =~ regular expressions, && and || inside
aliasalias [name[=value]...]: define or print aliases
bgbg [%job]: continue a stopped job in the background
breakbreak [n]: leave n enclosing loops
cdcd [-L|-P] [dir]: change directory; -L (default) keeps the logical path, -P resolves symlinks, cd - goes back
commandcommand name [arg...], command -v | -V name...: run a command without function lookup; -v/-V show how a name resolves
completecomplete [-abcdfuv] [-o default|dirnames] [-W words] [-F function] name..., complete -p [name...], complete -r [name...]: programmable completion
continuecontinue [n]: next iteration of n enclosing loops
echoecho [-neE] [arg...]: print the arguments
evaleval [arg...]: run the arguments as a command
execexec [command [arg...]], exec redirection...: replace the shell, or redirect it for good
exitexit [n]: leave the shell
exportexport [-p] [name[=value]...]: put variables in the environment; -p prints them
falsefalse: status 1
fgfg [%job]: bring a job to the foreground
getoptsgetopts optstring name [arg...]: parse options in a script
hashhash [-r] [name...]: remember where commands live; -r forgets
helphelp [-s] [pattern...]: synopsis and description of builtins and keywords; -s synopsis only
historyhistory [-c | -w | -r]: show the history; clear it, write or read the file
jobsjobs [-l | -p]: list background jobs, with pids, or pids only
killkill [-s signal | -signal] pid | %job ..., kill -l: send a signal; -l lists the names
locallocal [name[=value]...]: variables local to a function
logoutlogout: leave a login shell
pwdpwd [-L|-P]: print the working directory, logical or physical
readread [-r] name...: read a line into variables, split on IFS; -r keeps backslashes
readonlyreadonly [-p] [name[=value]...]: make variables read-only; -p prints them
returnreturn [n]: return from a function or sourced file
setset [-abCefhmnuvx] [-o option] [+...] [--] [arg...], set name=value: shell options and positional parameters, and hash’s own settings (Shell Options)
shiftshift [n]: drop positional parameters
timestimes: user and system times of the shell and its children
traptrap [action] [signal...], trap -l | -p: run commands on signals and at EXIT; -l lists the signals, -p prints the traps
truetrue: status 0
typetype [-afptP] name...: how a name would be interpreted; -t one word, -p/-P the file, -a every match, -f no functions
ulimitulimit [-HS] [-a | -cdfnstuv] [limit]: show or set resource limits
umaskumask [-S] [mode]: show or set the file creation mask; -S symbolic
unaliasunalias [-a] name...: remove aliases; -a all of them
unsetunset [-f | -v] name...: remove variables (-v) or functions (-f)
updateupdate: install a newer release of hash when there is one
waitwait [pid | %job ...]: wait for background jobs

Configuration Files

FileWhen Loaded
/etc/profileLogin shell
~/.hash_profileLogin shell
~/.profileLogin shell (fallback)
~/.hashrcInteractive shell
~/.hash_logoutLogin shell exit