Stupid Bash Tricks Part Two

In the last article, Stupid BASH Tricks One I went over escape codes, customizing the BASH prompt, and having fun with Fortune, and Cowsay. This time around we are going to cover BASH aliases, and end with a fork bomb.

##BASH Aliases Bash aliases are simply a way to create shortcut commands, abbreviation, or way to not need to remember long complicated commands. The Linux Documentation Project has a very good definition, which I’ve paraphrased.

When working with aliases there are two commands:

alias: Creates an alias ualias: Removes an alias

To make an alias use the following syntax:

1
alias name='command'

To make aliases permanent place them into your .bashrc file.

###Examples One of the ways in which an alias in .bashrc can be helpful is to change the behavior of Existing commands by running flags that are long to type out. ls

1
2
alias ls='ls -lha --color --group-directories-first'
alias ll='ls -la --color'

You can alias cd to go back up multiple levels.

cd

1
2
alias ..='cd ..'
alias ...=cd ../../

grep

1
alias grep='grep --color=auto'

ping

1
alias ping='ping -c 5'

rm

1
alias rm='rm -I --preserve-root'

mv

1
alias mv='mv -i'

cp

1
alias cp='cp -i'

reboot

1
alias reboot='sudo reboot'

Create Your Own Commands

1
2
alias mkpa='echo "$(date +%s | sha256sum | base64 | head -c 20)"'
alias mkpc='echo $(tr -cd [:alnum:][:punct:] < /dev/urandom | head -c 20 ; echo)'

Alaises are a neat feature of the BASH shell. What I’ve shown here are just a few examples to help you get started with them.

##FORK BOMB For my Final Stupid BASH trick, I give you, the Fork Bomb. A fork bomb works by creating processes until all of a system’s resources are used up. Fortunately, a BASH fork bomb can be mitigated, if your users have a limit on the amount of the system resources they are allowed to use. If a fork bomb takes out the system rebooting is the only way to clear it, and make it usable again. For all of its nastyness I’ve read that some system administrators run a BASH fork bomb as a test of their systems hardening.

###The Forkbomb Code

1
:(){ :|:& };:
:() - Defines an unnamed BASH function
{
|: - Calls the function twice & - Puts the process in the background } ; - End the function
  • Runs the function

DO NOT RUN THE FORK BOMB UNLESS YOU HAVE PROPERLY SECURED YOUR SYSTEM, or WANT TO TAKE IT DOWN.

Written on May 13, 2016