Let me show you some tricks on the Bash or Z shell that will help you with productivity.
You have probably come to the idea that Bash maintains a history of the commands you type, by default it keeps 1000 lines of code. You can view it on the terminal settings, under the “Behavior” tab, it should say “History size (in lines)”.
Now let’s get to the practicality. After we’ve typed plenty of commands, let’s explore the “history” command.
You can see that each command is numbered, so rather than retyping them, we can use the history expansion facility to rerun them easily by making use of the number.
Type the “!” character followed by the line number to execute the command.
To rerun the last executed command type “!!“.
By default, the command history is saved to the .bash_history file in the user home directory. Two environment variables control the history size: HISTSIZE and HISTFILESIZE.
HISTSIZE controls the number of commands stored in memory for the current session and HISTFILESIZE configures how many commands are kept in the history file. These variables can be edited according to our needs and saved to the Bash configuration file (.bashrc) that we will explore later.
Another useful trick is pressing CTRL + R, just like searching for a word on a page with CTRL + F, but this time in our history. It’s called reverse-i-search facility. The first result will be the most recent command containing the letter/word you’ve entered.
What are redirection and piping?
STDIN, STDOUT and STDERR
Every program run from the command line has three data streams connected to it that serve as communication channels with the external environment. These streams are defined as follows:
|0 Standard Input (STDIN)||Data fed into the program|
|1 Standard Output (STDOUT)||Output from the program (defaults to terminal)|
|2 Standard Error (STDERR)||Error messages (defaults to terminal)|
To say it the simplest way: redirection is used to redirect the Standard Output, Standard Input or Standard Error to either a file or stream, for example, ls > output.txt. (Note – if output.txt exists, it’ll be overwritten and it’s non-reversible)
Normally it would show/print the result/output to the terminal, but now we’ve redirected the Standard Output to the output.txt file, using the > operator.
┌──(kali㉿kali)-[~] └─$ cat output.txt Desktop Documents Downloads Music notes output.txt Pictures Public Templates Videos
We can also use redirection the “other way”. To send data the “other way”, which is from a file, we use the < operator. In the example below, we redirect the “wc” command’s Standard Input with data originating directly from the file we generated in the previous example. “wc -m” counts characters in the file.
Let’s redirect the Standard Error while using the file descriptor numbers (the red numbers on the table above) on our command line. We’ll redirect the error message to a file, by sticking the stream number 2 to the “>” operator.
┌──(kali㉿kali)-[~] └─$ cat file.txt 2> error.txt
┌──(kali㉿kali)-[~] └─$ cat error.txt cat: file.txt: No such file or directory
Pipes ( | ) are used to pass the output to another program or utility’s input. The pipe takes everything written to standard output from cat error.txt and sends it to the standard input of wc -m which then will print out to the standard output unless otherwise specified.
┌──(kali㉿kali)-[~] └─$ cat error.txt | wc -m 41
┌──(kali㉿kali)-[~] └─$ cat error.txt | wc -m > characters.txt
┌──(kali㉿kali)-[~] └─$ cat characters.txt 41