Basic Bash Script Cheatsheet
10/05/2021 Tags: LinuxBash is a Unix shell and a command processor that can read and execute commands from a file called a shell script. Like all Unix shells, it supports condition-testing and iteration ….etc. In addition, Bash has lots of different kinds of brackets, such as curly brackets, square brackets, parentheses. In this post, I was sorting out some information and hope these information will be helpful to you or anyone who is interested.
Parentheses “()”
List of Parentheses:
Single Parentheses
The first use is running commands inside in a subshell. This means that they run through all the commands inside, and then any variables declared or environment changes will get cleaned up after executing. Because it’s with in a subshell,
$ a='This string'
$ echo $a
This string
$ (a='Inside parentheses')
$ echo $a
This string
The second use is declaring arrays.
$ cheeses=('cheddar' 'swiss' 'brie')
$ echo ${cheeses[2]}
swiss
$ for cheese in $cheeses; do
$ for> echo "$cheese"
$ for> done
cheddar
swiss
brie
Double Parentheses
This is for use in integer arithmetic, such as assignment, logical operations, and mathematic operations like multiplication or modulo. But, do note that there is no output.
$ i = 4
$ (( i += 3 ))
$ echo $i
7
Dollar Single Parentheses
This is for interpolating a subshell command output into a string.
$ a = 5
$ b=$( a=1000; echo $a )
$ echo $b
1000
$ echo $a
5
Square Brackets “[]”
List of Square Brackets
Single Square Brackets
This is an alternate version of the built-in test. When executing the command, it could check for truthiness, like checking if a file exists or if it’s a directory.
$ touch my_friends.txt
if [ -f my_friends.txt]
then
echo "I'm loved!"
else
echo "I'm so alone"
fi
I'm loved
Double Square Brackets
Double square brackets support extended regular expression matching. Just use quotes around the second argument to force a raw match instead of a regex match.
$ pid=good
$ [[ $pie = ~d ]]; echo $?
0 // it matches the regex!
Braces “{}”
List of Braces
Function Braces
Functions are a little bit stranger in Bash than many other languages. First of all, there’re several ways to define them, that are all totally equivalent:
$ function hi_there() {
function> name="$1"
function> echo "HI $name"
function> }
or
$ hi_there() {
function> name="$1"
function> echo "HI $name"
function> }
$ hi_there shirong
HI shirong
Single Curly Braces
The first use for these braces is expression.
$ echo "I am "{cool,great,awesome}
I am cool I am great I am awesome
$ mv friends.txt{,.bak}
$ ls friends
friends.txt.bak
The second use is grouping commands.
Dollar Braces
The first use is for variable interpolation.
$ fruit=banana
$ echo ${fruit}ification
fruitification
The second use is variable manipulation.
$ function sign_in() {
function> name=$1
function> echo "Signing in as ${name:-$( whoami )}"
function> }
$ sign_in
Signing in as shi-rongliu
The third use is chopping off piece that match a pattern.
$ url=https://assertnotmagic.com/about
$ echo ${url#*/} // Remove from the front of matching pattern
/assertnotmagic.com/about
$ echo ${url%/*} // Remove from the back of matching pattern
https://assertnotmagic.com
=========== To be continued…. ==========
Reference
[1] Bash Brackets Quick Reference
[3] Bash Script 語法解析
[4] (StackExchange: creating a sequence of numbers, one per line in a file)(https://unix.stackexchange.com/questions/25592/creating-a-sequence-of-numbers-one-per-line-in-a-file)