codea.live
Search…
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.

1
$history 2 1 ls 3 2 echo "Testing123" 4 3 ls 5 4 history Copied! We can then execute them with !<command_number>. 1$ !2
2
echo Testing123
3
Testing123
Copied!

## How to Clear Bash History

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

1
$history -c; history -w; Copied! .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 1 echo "Testing123" Copied! Then we reload the terminal with bash we get this... 1 bash 2 Testing123 Copied! ### 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 1 PS1='[\033[1;31m]\u[\033[m]@[\033[1;32m]\h[\033[m]:[\033[1;36m]\w[\033[m]$[\033[m] '
Copied!
Now the prefix has colours!

# Shorten Commands - Bash Aliases

1
alias <name>="<command>"
Copied!

### Example

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

## 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
1
if [ -f $HOME/.bash_aliases ] 2 then 3 .$HOME/.bash_aliases
4
fi
Copied!

# 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.
1
$man grep | grep -e '-e ' Copied! ## Anything - Star or Asterisk (*) Used to search for any character, zero or more times. Example 1 Example 2 Example 3 This will find everything in the current directory. 1$ find *
2
1.txt
3
2.txt
4
3.txt
5
a.png
6
b.png
7
c.png
8
error.log
9
log.log
10
log.txt
11
20-04-21-213100.log
Copied!
This is also useful if you want to find anything that matches a file extension.
1
$find *.png Copied! This is used to find a file with a specific name and any file extension. 1$ find log.*
2
log.log
3
log.txt
Copied!

## Unknown - Question Mark (?)

Used to search for a fixed number of characters. '?' can indicate an unknown character.
Example 1
Example 2
Example 3
This will find any .txt file with a 1 character name.
1
$find ?.txt 2 1.txt 3 2.txt 4 3.txt Copied! This will find any file name, with a 3 letter file extension, with 'x' in the middle. 1$ find *.?x?
2
1.txt
3
2.txt
4
3.txt
5
log.txt
Copied!
In this example, this operator can be useful for finding log files at a specific hour.
1
$find 20-04-21-21????.log 2 20-04-21-213100.log Copied! ## 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 This is useful for finding files that start with any number from 0-9. This is called a range. 1$ find [0-9].txt
2
1.txt
3
2.txt
4
3.txt
Copied!
This also works with the alphabet!
1
$find [a-z].png 2 a.png 3 b.png 4 c.png Copied! The square bracket can combine characters to make a set. The character can only be any character within the set. 1$ find [23ac].???
2
2.txt
3
3.txt
4
a.png
5
c.png
Copied!

# Piping

## What is a pipeline?

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

1
$echo "Hello Earth" 2 Hello Earth Copied! [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 1$ echo "Hello Earth" 0>> echo
2
$Hello Earth Copied! This command puts the output of the first command into the second command. 1$ echo "Hello Earth" 1>> output.txt
2
$cat output.txt 3 Hello Earth Copied! This puts the output of the first command into a file (output.txt). 1$ cat '' 2>> error.txt
2
$cat error.txt 3 cat: '': No such file or directory Copied! 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. 1 A ; B # Run A and then B, regardless of success of A 2 A && B # Run B if A succeeded 3 A || B # Run B if A failed 4 A & # Run A in background. Copied! More on Logical Operators # Brace Expansion Brace expansion is used to generate strings. Combinations Prefix/Suffix List Command Example 1$ echo {a..z}
2
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
3
$echo {z..w} 4 z y x w 5$ echo {1..10}
6
1 2 3 4 5 6 7 8 9 10
7
$echo {3..-1} 8 3 2 1 0 -1 Copied! 1$ echo a{a..f}
2
aa ab ac ad ae af
3
$echo {1..3} && echo {99,100} 4 1 2 3 99 100 Copied! 1$ ls {Desktop,Documents}
2
Desktop:
3
'Visual Studio Code.lnk'* desktop.ini
4
5
Documents:
6
'My Music'@ 'My Pictures'@ 'My Videos'@ desktop.ini
Copied!

1
$echo {a,{a..c},{a..c}} 2 a a b c a b c Copied! Make sure there are no spaces around commas, otherwise brace expansion will not work. ## Multiple Brace Expansion Brace yourself for a basic maths lesson. $3^2=3*3=9$ 1$ echo {1..3} # 3*1 = 3 combinations
2
1 2 3
3
$echo {1..3}{1..3} # 3*3 = 9 combinations 4 11 12 13 21 22 23 31 32 33 5$ echo {1..3}{1..3}{1..3} # 3*3*3 = 27 combinations
6
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
Copied!

# ​Challenge - Make a Today Command​

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.
Last modified 6mo ago