A Cheat Sheet for your Bash#

Hey all! This week, I want to talk a bit about a new love of mine: shell.

While Python was my first programming language, over the years, I have been doing more and more things in shell. I specifically use Bash, and I always seemed to reach for Google whenever I had to do anything remotely complex and beyond the basics.

Rather than dive into a lengthy tutorial and description of various shell features, I wanted to share a “cheat sheet” of my favorites.

Parameter Expansion#

First, to do something with a variable, we’ll use ${ }.

Here’s an example of what that would look like:


x='hello world'
echo '$x' $x
$x hello world

Parameter Expansion Flags#

To flag doing something with variable, use ${( ) …}:


x='hello world'

# String slicing
echo '${x:0:8}' ${x:0:5}
echo '${x:5  }' ${x:5}

# Prefix/Suffix Removal
echo '${x#hello}' ${x#hello} # match beginning; delete shortest match
echo '${x%world}' ${x%world} # match trailing; delete shortest match
# Instead of matching on strings, you can also match on patterns
# https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html

# Substitution
echo '${x/hello/hi there}' ${x/hello/hi there}
${x:0:8} hello
${x:5  } world
${x#hello} world
${x%world} hello
${x/hello/hi there} hi there world

Another example:


x='hElLo WoRlD'
echo '$x' $x

echo ${x@U} # convert all characters to uppercase
echo ${x@u} # convert first characters to uppercase
echo ${x@L} # convert all characters to lowercase

echo ${x@Q} # quote the parameter so it can be reused
$x hElLo WoRlD
hello world
'hElLo WoRlD'

Command Substitution#

To swap a command output for text, use $( ):


echo "Numbers from 1-5: $(seq -s ' ' 1 5)"
Numbers from 1-5: 1 2 3 4 5

Arithmetic Expansion#

To compute arithmetic operations, use $(( )):


echo 1 + 2 = $(( 1 + 2 ))
1 + 2 = 3


To redirect data into/out of stdin/stdout/stderr of a process, do the following:


seq 1 10 > ${temp} && wc -l ${temp} # create a file ${temp} and write the output of `seq` to it
seq 1 5 >> /tmp/test.txt && wc -l ${temp} # add another 5 lines to the file
10 /tmp/test.txt
15 /tmp/test.txt

# wc works by reading a file, we could pipe this string into `wc`, or instead
# we can use a herestring that acts like a file

wc -w <<<'this is a sentence'

Process Substitution#

Finally, to substitute a process for a filename/file descriptor, use <( ) >( ):


echo <( seq 1 10 )   # note that we get a file descriptor out!
grep 3 <( seq 1 10 ) # grep accepts a file as its input- no pipes needed

Wrap Up#

I just wanted to share this brief write up of some common syntax I aim to incorporate more of when I use shell. These little tidbits are wildly useful in a variety of contexts and should be kept in mind. Hope you find this useful! Talk to you again next week.