Bash - Do More With Less

More advanced bash concepts like piping, aliases, and more explained in one guide, that will help you save time and energy.

In This Guide...

After this guide you will:

Prerequisites: know the basics of bash.

Time Travel - Bash History

Here you will learn how to go back in time and execute previous commands. This is how....

The history command lets us see previously executed commands.

$ history
1 ls
2 echo "Testing123"
3 ls
4 history

We can then execute them with !<command_number>.

$ !2
echo Testing123
Testing123

How to Clear Bash History

Sometimes history may get messy, and mine had 500+ previous commands, before making this tutorial.

Syntax

$ history -c; history -w;

.bash_history stores bash history.

.bashrc

.bashrc is a Bash shell script that Bash runs whenever it is started interactively.

Hence when .bashrc looks like this...

~/.bashrc
echo "Testing123"

Then we reload the terminal with bash we get this...

Testing123

Every time the bash terminal is loaded, .bashrc is executed.

Customize the Terminal - Command Prefix

.bashrc can be used for many things including customizing the bash command prefix.

This is how I did it:

~/.bashrc
PS1='[\033[1;31m]\u[\033[m]@[\033[1;32m]\h[\033[m]:[\033[1;36m]\w[\033[m]$[\033[m] '
Now the prefix has colours!

Shorten Commands - Bash Aliases

alias <name>="<command>"

Example

~/.bashrc
alias testing123="echo 'It Worked!'"
$ bash # or manually reopen the terminal
$ testing123
It Worked!

Use .bash_aliases for aliases

In the default folder (~), we can create special file called .bash_aliases. This is a conventional file to contain bash aliases.

This is optional, but the code in .bashrc below should load the aliases in the file.

~/.bashrc
if [ -f $HOME/.bash_aliases ]
then
. $HOME/.bash_aliases
fi

Wildcards

Wildcard characters are used to define patterns for searching text on data in bash.

The find command is used for finding files or folders within a folder. Type man find for more info.

Grep - Match Patterns in Strings

The code below will find the line containing '-e ', and output it.

$ man grep | grep -e '-e '

Anything - Star or Asterisk (*)

Used to search for any character, zero or more times.

Example 1
Example 2
Example 3
Example 1

This will find everything in the current directory.

$ find *
1.txt
2.txt
3.txt
a.png
b.png
c.png
error.log
log.log
log.txt
20-04-21-213100.log
Example 2

This is also useful if you want to find anything that matches a file extension.

$ find *.png
Example 3

This is used to find a file with a specific name and any file extension.

$ find log.*
log.log
log.txt

Unknown - Question Mark (?)

Used to search for a fixed number of characters. '?' can indicate an unknown character.

Example 1
Example 2
Example 3
Example 1

This will find any .txt file with a 1 character name.

$ find ?.txt
1.txt
2.txt
3.txt
Example 2

This will find any file name, with a 3 letter file extension, with 'x' in the middle.

$ find *.?x?
1.txt
2.txt
3.txt
log.txt
Example 3

In this example, this operator can be useful for finding log files at a specific hour.

$ find 20-04-21-21????.log
20-04-21-213100.log

Range - Square Brackets ([])

Used to search for a range of characters (i.e. 0-9, a-z).

Ranges are case sensitive. Therefore, [a-z] will not match 'Z', but [A-Z] will.

Example 1
Example 2
Example 3
Example 1

This is useful for finding files that start with any number from 0-9. This is called a range.

$ find [0-9].txt
1.txt
2.txt
3.txt
Example 2

This also works with the alphabet!

$ find [a-z].png
a.png
b.png
c.png
Example 3

The square bracket can combine characters to make a set. The character can only be any character within the set.

$ find [23ac].???
2.txt
3.txt
a.png
c.png

Piping

What is a pipeline?

In computer science, a pipeline is a series of elements, where each one is connected to the next.

The Command Pipeline

$ echo "Hello Earth"
Hello Earth

[0] Standard Input - typed command and its arguments; stdin. [1] Standard Output - normal output from a command; stdout. [2] Standard Error - error output from a command; stderr.

We Can Control Command Output

$ echo "Hello Earth" 0>> echo
$ Hello Earth

This command puts the output of the first command into the second command.

$ echo "Hello Earth" 1>> output.txt
$ cat output.txt
Hello Earth

This puts the output of the first command into a file (output.txt).

$ cat '' 2>> error.txt
$ cat error.txt
cat: '': No such file or directory

This puts the error from the cat command, into a file (error.txt).

Logical Operators Can Help Too

Logical operators are used for command pipelines.

A ; B # Run A and then B, regardless of success of A
A && B # Run B if A succeeded
A || B # Run B if A failed
A & # Run A in background.

Brace Expansion

Brace expansion is used to generate strings.

Combinations
Prefix/Suffix
List Command Example
Combinations
$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo {z..w}
z y x w
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ echo {3..-1}
3 2 1 0 -1
Prefix/Suffix
$ echo a{a..f}
aa ab ac ad ae af
$ echo {1..3} && echo {99,100}
1 2 3 99 100
List Command Example
$ ls {Desktop,Documents}
Desktop:
'Visual Studio Code.lnk'* desktop.ini
Documents:
'My Music'@ 'My Pictures'@ 'My Videos'@ desktop.ini

Nested Brace Expansion

$ echo {a,{a..c},{a..c}}
a a b c a b c

Make sure there are no spaces around commas, otherwise brace expansion will not work.

Multiple Brace Expansion

Brace yourself for a basic maths lesson.

32=33=93^2=3*3=9
$ echo {1..3} # 3*1 = 3 combinations
1 2 3
$ echo {1..3}{1..3} # 3*3 = 9 combinations
11 12 13 21 22 23 31 32 33
$ echo {1..3}{1..3}{1..3} # 3*3*3 = 27 combinations
111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 232 233 311 312 313 321 322 323 331 332 333

Doing is the most effective way of learning, and will help reinforce your knowledge and understanding.

This content is from an upcoming Raspberry Pi hosting course.