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-z
A-Z
0-9
range: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~user
home directory of theuser
~+
$PWD
,~-
$OLDPWD
~N
~+N
N=th previous directory in the directory stack, =~-N
=N=th oldest directory in the directory stack- The directory stack is created with
pushd
andpopd
- The directory stack is created with
type
- Shell builtin that returns the type of the commands.
shell keyword
shell 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 var
define variable local to a function.
2.1.2. Shell Parameter Expansion
$VARIABLE
the value of theVARIABLE
${VARIABLE=DEFAULT}
set toDEFAULT
whenVARIABLE
is unset.${VARIABLE-DEFAULT}
use default whenVARIABLE
is unset${VARIABLE:-DEFAULT}
useDEFAULT
even when theVARIABLE
is set to null, like
var=
.${VARIABLE+ALTERNATIVE}
use the alternative whenVARIABLE
is set.${VARIABLE?ERROR_MESSAGE}
printERROR_MESSAGE
and exit with exit status1
ifVARIABLE
is unset.${VARIABLE#PATTERN}
remove the shortestPATTERN
in the prefix.##
the longest pattern.- use
%
,%%
for suffix.
${VARIABLE/PATTERN/REPLACEMENT}
//PATERN
for global replacement,#PATTERN
,%PATTERN
to
match prefix and suffix.
${VARIABLE:POSITION}
value form thePOSITION
POSITION:LENGTH
is also possible.
${#VARIABLE}
the length of theVARIABLE
${!POINTER_VARIABLE}
indirect reference. The value of theVARIABLE
whose 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 statusi
interactive- The Set Builtin Bash Reference Manual
$_
the last evaluated script or script file.$#
$0
filename of the script file. set to the shell binary path/usr/bin/bash
if ran interactively.$N
the 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.shift
built-in command shift the argument to the left by
one.
$BASH
the shell binary path$FUNCNAME
defined within a function to the name of the funciton.IFS
internal field separator- default is
␣\t\n
- default is
$TERM
current terminal value
2.2. Arithmetic Expansion
((STATEMENT))
$((EXPRESSION))
expr
let
evaluate 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 =~ REGEX
is 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.
NULL
or""
empty string is false.! EXPRESSION
EXPRESSION1 {-a|-o} EXPRESSION2
( EXPRESSION )
STRING1 {==|! =} STRING2
-z STRING
the length is zero,-n STRING
the length is nonzero.- Globbing is available
INTEGER1 {-eq|-ge|-gt|-le|-lt|-ne} INTEGER2
FILE1 {-ef|-nt|-ot} FILE2
same device and inode numbers, newer than, older than-e FILE
file exists- and it is
-b
block,-c
character,-d
directory,-f
regular file,-h
-L
symbolic link,-s
nonzero size.,-S
socket,-p
named pipe,-r
readable,-w
writable.
- 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
,done
are 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
exec
fork a child process and execute, the script terminates right away after this.eval
evaluate 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
command
and substitute the output directly. - With few modification, such as no parsing for
\\
.
3.2.2. <(command)
- Executes the
command
and store it in/dev/fd/N
and 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
echo
printf
read VARIABLE
read 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
noclobber
shopt 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
n
for both read and write.
- file is opened as file descriptor
[n-0]<<[-]DELIMITER
- Here Documents
- The delimiter is quote removed.
- Write the content directly into the command line until specified
DELIMITER
appears.[n-0]<<<WORD
WORD
gets expanded and supplied directly.
[n-0]<&{m|-}
- File descriptor <-> File Descriptor
n
becomes the copy of the file descriptorm
.n
is closed
if
-
.[n-1]>&{m|-}
n
becomes the copy of them
[n-0]<&m-
The file descriptorm
is moved ton
, andm
is
removed.
[n-1]>&m-
m
is 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
cd
pwd
print current working directorypushd
popd
5. History
5.1. Builtins
fc
edit and execute history entries (POSIX)-e
set editor-l
list history-s
execute 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
n
refers to then
th command from the initialization. - negative number
-n
refers to then
th command previous to current line.
- positive number
!PREFIX
match last command withPREFIX
:
Word DesignatorN
n 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
complete
shell builtin (Bash only)-p NAME
the completion specification forNAME
- search compspec for the full pathname, search compspec for the
portion after final slash, then compspec defined as the default with
-D
is used.-G GLOBPATTERN
-W WORDLIST NAME
-F FUNCTION NAME
the completions are returned inCOMPREPLY
array variable.
-C COMMAND
-P PREFIX
-S SUFFIX
bash-completion
- This package uses
complete
to 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_load
loads 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/inputrc
and~/.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
PS1
is the prompt format.PS2
is the continuation prompt format.
6.5. Starship
- It sets the prompt based on the context, using the
starship
binary 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
FORMAT
can 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 = FORMAT
symbol =
style =
pipestatus = true|false
pipestatus_format = FORMAT
pipestatus_segment_format = FORMAT
disabled = true|false
python
symbol =
- 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