Louis Better than before

Basic Bash Script Cheatsheet

Bash 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:

  1. Single Parentheses
  2. Double Parentheses
  3. Dollar Single 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]}

$ for cheese in $cheeses; do
$ for>    echo "$cheese"
$ for> done

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

Dollar Single Parentheses

This is for interpolating a subshell command output into a string.

$ a = 5
$ b=$( a=1000; echo $a )
$ echo $b
$ echo $a

Square Brackets “[]”

List of Square Brackets

  1. Single Square Brackets
  2. Double 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]
    echo "I'm loved!"
    echo "I'm so alone"
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

  1. Function Braces
  2. Single Curly Braces
  3. Dollar 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> }


$ 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

The second use is grouping commands.

Dollar Braces

The first use is for variable interpolation.

$ fruit=banana
$ echo ${fruit}ification

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

$ echo ${url%/*} // Remove from the back of matching pattern

=========== To be continued…. ==========


[1] Bash Brackets Quick Reference

[2] Wike: Bash(Unix shell)

[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)