Day 6 OSCP | Bash Tricks, Redirection and Piping

Reading Time: ( Word Count: )

December 11, 2020

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.

  ┌──(kali㉿kali)-[~]      └─$ history 1 echo $$ 2 exit 3 echo $globalvar 4 env 5 clear 6 sudo traceroute -I 7 sudo traceroute -T 8 sudo traceroute -U ...


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.

  ┌──(kali㉿kali)-[~]      └─$ !3 echo $globalvar Global Var


To rerun the last executed command type “!!“.

  ┌──(kali㉿kali)-[~]      └─$ !! echo $globalvar Global Var


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.

  ┌──(kali㉿kali)-[~]    (reverse-i-search)`h': history


What are redirection and piping?



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:

 Stream Name  Description
 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)-[~]      └─$ ls > output.txt
  ┌──(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.

  ┌──(kali㉿kali)-[~]      └─$ wc -m 84


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 cat: file.txt: No such file or directory
  ┌──(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 cat: err.txt: No such file or directory
  ┌──(kali㉿kali)-[~]      └─$ cat error.txt | wc -m 41
  ┌──(kali㉿kali)-[~]      └─$ cat error.txt | wc -m > characters.txt
  ┌──(kali㉿kali)-[~]      └─$ cat characters.txt 41

Other interesting articles

Leetcode vs Hackerrank vs Topcoder: Difference Explained

Leetcode vs Hackerrank vs Topcoder: Difference Explained

Are you the one who wants to avail a software development job? Or you are a recruiter looking forward to test a ...
Are Older Android Versions Safe? 5 Security Tips

Are Older Android Versions Safe? 5 Security Tips

Are you an Android phone user? Do you find it hard to keep your phone safe from malware attacks? If yes, this ...
Cybersecurity Vs Network Engineer – Top 3 Differences You Should Know

Cybersecurity Vs Network Engineer – Top 3 Differences You Should Know

In this digital age, without the existence of cybersecurity, businesses on the web cannot survive. Now and then, we
What Programming Language Do Hackers Use in 2021?

What Programming Language Do Hackers Use in 2021?

Before diving deep into the programming languages hackers tend to use, it depends on the type of hacking ...
1 Comment
  1. Appreciate your content!


Submit a Comment

Your email address will not be published. Required fields are marked *

error: Alert: Content is protected !!