Bash
Table of Contents
- Bourne Again Shell
1. Parse
- Bash does not have types.
- Quotes are used to delimit the value
1.1. Single Quote
It is a literal string. No need to escape. No substitutionse letter is in the bracket.
a-zA-Z0-9range:alnum::alpha::ascii::digit::space::lower::upper:…=c=match the equivalence class with the collation weight..symbol.match collating symbol.
?(PATTERNS)match once,*(PATTERNS)match zero or more times,+(PATTERNS)match one or more times,@(PATTERNS)match one of the patterns,!(PATTERNS)does not match any pattern.
1.2. Brace Expansion
{,STRING1,STRING2,...}- Expand into words containing each
STRING
- Expand into words containing each
{START..END[..STEP]}for sequences.
1.3. Tilde Expansion
~home directory of the current user~userhome directory of theuser~+$PWD,~-$OLDPWD~N~+NN=th previous directory in the directory stack, =~-N=N=th oldest directory in the directory stack- The directory stack is created with
pushdandpopd
- The directory stack is created with
type- Shell builtin that returns the type of the commands.
shell keywordshell builtin
2. Syntax
#is for comment
2.1. Variable
*and@expands differently, on double-quoted expansion. They would expand into multiple strings regardless without double-quotes.@: the elements expand into multiple strings, with the first
argument and last argument joined with the surrounding strings.
*: the elements expand into single string joined by
${IFS:0:1}.
2.1.1. Definition
- Number:
var=20 - Array:
array=(1 2 apple) local vardefine variable local to a function.
2.1.2. Shell Parameter Expansion
$VARIABLEthe value of theVARIABLE${VARIABLE=DEFAULT}set toDEFAULTwhenVARIABLEis unset.${VARIABLE-DEFAULT}use default whenVARIABLEis unset${VARIABLE:-DEFAULT}useDEFAULTeven when theVARIABLE
is set to null, like
var=.${VARIABLE+ALTERNATIVE}use the alternative whenVARIABLEis set.${VARIABLE?ERROR_MESSAGE}printERROR_MESSAGEand exit with exit status1ifVARIABLEis unset.${VARIABLE#PATTERN}remove the shortestPATTERNin the prefix.##the longest pattern.- use
%,%%for suffix.
${VARIABLE/PATTERN/REPLACEMENT}//PATERNfor global replacement,#PATTERN,%PATTERNto
match prefix and suffix.
${VARIABLE:POSITION}value form thePOSITIONPOSITION:LENGTHis also possible.
${#VARIABLE}the length of theVARIABLE${!POINTER_VARIABLE}indirect reference. The value of theVARIABLEwhose name is the value of thePOINTER_VARIABLE.${!VARIABLE_PREFIX*}the list of the name of the variables withVARIABLE_PREFIX.@is also possible instead of*, for separated list.
${array[N]}N=th elements of the =array${array[*]}and${array[@]}to list all the elements.
${!array[N]}the dereference of theN=th element of the =array${!array[*]}and${!array[@]}list the indices of the array.
2.1.3. Special Parameters
- #+BEGINNOTE
${VARIABLE}is still valiid. #+ENDNOTE $?Represents the last exit code.$$Current shell PID.$!Current session ID.$-shell statusiinteractive- The Set Builtin Bash Reference Manual
$_the last evaluated script or script file.$#$0filename of the script file. set to the shell binary path/usr/bin/bashif ran interactively.$Nthe Nth parameter passed in as a argument.$#is the number of arguments.$*every arguments in a single string separated by a
delimiter specified by
IFS$@every arguments in list of strings.set ARG1 ARG2 ...sets the arguments manually.shiftbuilt-in command shift the argument to the left by
one.
$BASHthe shell binary path$FUNCNAMEdefined within a function to the name of the funciton.IFSinternal field separator- default is
␣\t\n
- default is
$TERMcurrent terminal value
2.2. Arithmetic Expansion
((STATEMENT))$((EXPRESSION))exprletevaluate arithmetic statements
2.3. Control Flow
2.3.1. ;
- It represents the end of a statement. It can be omitted when newline is used.
- Sequentially execute unconditionally
2.3.2. && ||
- Execute commands sequentially.
&&The next command on the same line is executed only if the previous commands exited with0||the next command is executed only if the previous commands exited with exit code other than0
2.3.3. If
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ] [ else COMMANDS; ] fi
2.3.3.1. [[
- The string variable does not need to be quoted, and
||,&&for logical operation also works. - It runs faster than
[, because it is a shell keyword. Implements additional functionalities like regular expression.STRING =~ REGEXis available for matching.
2.3.3.2. test [
- A command that exit 0 if true, exit 1 if false.
- shell builtin (external)
/usr/bin/test.test(1) - shell builtin (external)
/usr/bin/[: it takes]as its argument.[was once a symlink totest- POSIX compliant.
NULLor""empty string is false.! EXPRESSIONEXPRESSION1 {-a|-o} EXPRESSION2( EXPRESSION )STRING1 {==|! =} STRING2-z STRINGthe length is zero,-n STRINGthe length is nonzero.- Globbing is available
INTEGER1 {-eq|-ge|-gt|-le|-lt|-ne} INTEGER2FILE1 {-ef|-nt|-ot} FILE2same device and inode numbers, newer than, older than-e FILEfile exists- and it is
-bblock,-ccharacter,-ddirectory,-fregular file,-h-Lsymbolic link,-snonzero size.,-Ssocket,-pnamed pipe,-rreadable,-wwritable.
- and it is
2.3.4. Case
case STRING in
PATTERN)
COMMANDS
PATTERN)
COMMANDS
*)
COMMANDS
esac
2.3.5. While
while COMMANDS; do COMMANDS; done
2.3.6. For
for NAME [ in WORDS ... ] ; do COMMANDS; done
for (( exp1; exp2; exp3 )); do COMMANDS; done
do,doneare for executing block of commands.
2.3.7. Select
select NAME in WORDS; do COMMANDS; done
After the execution with the selection, it repeats from the
beginning. break is required.
2.4. Function
$RANDOM generate a pseudorandom number.
2.4.1. Definition
func_name () { COMMANDS }
The parameters are passed using $N
2.4.2. Call
func_name ARG1 ARG2 ...
3. Execution
source.- Execute a script file
execfork a child process and execute, the script terminates right away after this.evalevaluate the string as a command.
3.1. Grouping
( COMMANDS )the commands are run in a subshell, and the output is returned in one string.(is an operator. whitespace is not necessary.
{ COMMANDS }the commands are run in the current context.{is a shell keyword. Must be separated by whitespace.
3.2. Command Substitution
- Plain inline
- It leaves the output of commands with multiple outputs as is.
3.2.1. $(command)
- Execute the
commandand substitute the output directly. - With few modification, such as no parsing for
\\.
3.2.2. <(command)
- Executes the
commandand store it in/dev/fd/Nand return the filename, so that other commands can read from it.
3.3. Job Control
3.3.1. &
- Append at the end of a line
&, to run it in the background.bash $ background process &
3.3.2. Ctrl-Z
- Pause the currently executing process and put it in the background. It resembles the good-old ((651035dd-c85c-4ab9-adf0-c6938a3307d3)).
3.3.3. bg
- It controls background processes
bash # [1] background-process [stopped] bg %1 # starts background-process - Refer a process with the prefix
%.
3.3.4. fg
- It brings a background process to the foreground.
3.3.5. jobs
- Show the list of background processes
3.3.6. kill
- Terminate a process
4. Utilities
4.1. Input/Oupput
echoprintfread VARIABLEread the user input into theVARIABLE
4.2. Environment Variable Management
export- shell builtin (internal)
export(1P)help export- set export attribute of a shell variables.
- The exported variables are then set as the environment variables
of the child processes.
4.3. Redirection and Piping
- It applies to the commands that is being run. Not the shell itself.
[n-1]>[|]FILENAME- File Descriptor <-> File
- Write the stdout(1) of previous command to a file.
- If
noclobbershopt is set, it does not write to a existing
file,
>|force the write.[n-0]<FILENAME- Open it as
fd/n
- Open it as
[n-1]>>FILENAME- Append the stdout(1) of previous command to a file.
&>FILENAME- Redirect both the stdout(1) and stderr(2) to the file.
&>>FILENAME- Append
[n-0]<>FILENAME- file is opened as file descriptor
nfor both read and write.
- file is opened as file descriptor
[n-0]<<[-]DELIMITER- Here Documents
- Write the content directly into the command line until specified
DELIMITERappears. - If the delimiter is quoted (
"<DELIMITER>"), text is used verbatim <<-makes it indent aware, and remove it.
[n-0]<<<WORDWORDgets expanded and supplied directly.
[n-0]<&{m|-}- File descriptor <-> File Descriptor
nbecomes the copy of the file descriptorm.nis closed if-.[n-1]>&{m|-}nbecomes the copy of them[n-0]<&m-The file descriptormis moved ton, andmis removed.[n-1]>&m-mis moved ton
|- Take the stdout(1) of previous command and feed it to the stdin(0) of the next command.
4.4. Working Directory Management
cdpushdpopdcd -Nto pop to Nth directory in stack.<tab>to see the candidates.
pwdprint current working directory
5. History
5.1. Builtins
fcedit and execute history entries (POSIX)-eset editor-llist history-sexecute last match
history- Manage history entries
5.2. History Expansion
They have the form EVENT[WORD|MODIFIER]
- Event Designator
!!- It refers to the previous command.
!#- Every commands before this token, on the same line.
!N- positive number
nrefers to thenth command from the initialization. - negative number
-nrefers to thenth command previous to current line.
- positive number
!PREFIXmatch last command withPREFIX
:Word DesignatorNn th word^,$first or last argument
:modifierss/.../.../replace
5.3. Quick Substitution
^OLD^NEW[^|\n]- Substitute on the previous command
6. Configuration
shopt- Set shell options.
- OPTIONS
globstar- If set,
**matches the entire filepath recursively. - It doesn't work when used with other patterns.
- If set,
6.1. Configuration Files
/etc/bash.bashrc~/.bashrc- Only loaded when the shell is interactive (
$- != *i*)
- Only loaded when the shell is interactive (
6.2. Completion
completeshell builtin (Bash only)-p NAMEthe completion specification forNAME- search compspec for the full pathname, search compspec for the
portion after final slash, then compspec defined as the default with
-Dis used.-G GLOBPATTERN-W WORDLIST NAME-F FUNCTION NAMEthe completions are returned inCOMPREPLY
array variable.
-C COMMAND-P PREFIX-S SUFFIX
bash-completion- This package uses
completeto make additional autocompletion. - It is bunch of shell scripts, that needs to be sourced on
startup. The entry point is
/usr/share/bash-completion/bash_completion._comp_complete_loadloads the additional compspec fromCMD
or
CMD.bash, that is not set by default, defined in~/.local/share/bash-completion/completions//usr/share/bash-completion/completions/
- This package uses
6.3. Readline
bind(Bash only)- Configure the Readline settings
-m {emacs|vi|...}set the keybindings-v-V, (human readable) list Readline variables.
- Bash uses Readline library for its input. It allows the user to modify the commands in place.
- The configuration file is
/etc/inputrcand~/.inputrc- The configuration is in the form
set VAR VAL
- The configuration is in the form
- Emacs and Vim insert mode bindings
<C-r><C-s>search<C-w>delete back a word
6.4. Prompt
PS1is the prompt format.PS2is the continuation prompt format.
6.5. Starship
- It sets the prompt based on the context, using the
starshipbinary to generate various shell scripts and prompt formats. - It uses ((669c3baf-f6d5-4176-86b3-139a00084b7f)).
- Starship is compatible with many other shells, including , , Powershell, etc.
6.5.1. User Interface
6.5.1.1. git
+: Added?: Untrackedx: Deleted!: Modified$: Stashed=: Conflict≫: Renamed↑: To be upstreamed
6.5.2. Configuration
- There are prompt wide options and variables that can be used as part of the options
the prompt-wide variables are generated from modules which have their own options and variables that can be used as the part of the options within a module.
- Modules are specified under a TOML section named by the
module name.
- The configuration is stored in
~/.config/starship.toml - The
FORMATcan be specified by composing text group[FORMAT_STRING](STYLE_STRING), variables$VARIABLE, and conditional format strings(...$VARIABLES...)that is invisible when all variables are empty. - For some options that matches the values, negative matching
prefixed with
!is available. format =- the default format is available in
$all. The variables
will be overrided, and not be duplicated.
- the default format is available in
6.5.2.1. Modules
status$status,$common_meaning,$signal_number,
$signal_name,$pipestatus(inpipestatus_format =),$symbol,$style(in style string)format = FORMATsymbol =style =pipestatus = true|falsepipestatus_format = FORMATpipestatus_segment_format = FORMATdisabled = true|false
pythonsymbol =
- The config files are reloaded automatically on save.
- See Configuration | Starship
7. Reference
- The bash built-in commands are shown by
help - Advanced Bash-Scripting Guide
- Top Bash Reference Manual