fancy bash image

Want some bash-foo?!

Oneliners, bashfoo and other useful tricks ;)
/sbin/ip (ifconfig is deprecated)

Many of linux users are comfortable with ifconfig or route, but this tool is deprecated! The (imho better) tool is ip, which is contained in package iproute since Ubuntu 13.10 and Debian 7. I will give you a short tutorial about using /sbin/ip command.

In most parameters, one-letter abbreviation is allowed. Those commands are written as you would speak it out:

Bring interface up / down:

# Old ifconfig syntax: ifconfig eth0 up
# New ip syntax:
$ ip link set eth0 down
$ ip l s eth0 down

Assign ip address to interface:

# Old ifconfig syntax: ifconfig eth0 netmask
# New ip syntax:
$ ip addr add dev eth0
$ ip a a dev eth0

Flush interface:

# Old ifconfig syntax: ifconfig eth0
# New ip syntax:
$ ip addr flush eth0
$ ip a f eth0

Add new default route:

# Old ifconfig syntax: route add default gw eth0
# New ip syntax:
$ ip route add default via
$ ip r a default via

It might appear it is more to type, but if you work with this command a few times you will see, that it's much easier and you can be smart-ass if you see someone typing ifconfig or route :P


Simple portscan: nc -vnzw1 80

Perform a GET request to target:

echo -e "GET / HTTP/1.1\r\nHost:\r\n\r\n" | nc 80

vi or cd

Add the following piece of code to your .bashrc

function vc {
CMDLINE=$(history 2 | head -1 | awk -F " " '{print $NF}')
[[ -d $CMDLINE ]] && cd $CMDLINE

What does this code do?!

It takes the last argument of the previous command and saves this string in variable $CMDLINE. If this string is a valid file (-f), it opens the file with the default editor (assuming your $EDITOR variable is set). If not, it checks if it's a directory, if so it changes from the current to the target directory. In my case, I named the function to vc (vim or cd) ;)

What's the usecase?!

grep stylesheet /var/www/html/mysite/subtree/index.html # Assume you want to edit the file now...
vc # Opens $EDITOR with /var/www/html/mysite/subtree/index.html
--- or ---
ls -lahtr /etc/nginx/sites-available # Assume you want to change in this directory now...
vc # Changes from the current directory to /etc/nginx/sites-available
--- or ---
cat ../../modules/database/db-connect-params.php

Brace Expansion

There's a cool bash feature called brace expansion. Let me give you an example: echo {a..z} will print letters a to z. Or you can echo {1..25} which will print digits from 1 to 25.
You can also use it in commands: ls -l /etc/{passwd,shadow} will do an ls -l on /etc/passwd AND /etc/shadow.
Or perform a portscan on ip range (without nmap):

$ for i in {1..254}; do nc -vnzw1 192.168.1.$i 80 2>&1 | grep open; done

This will show you all hosts with port 80 open in your subnet.


When I started using vim, I learned to avoid using the arrow keys to increase efficiency. My fingers are on the homerow of the keyboard, when I want to reach the arrow keys I have to leave the home row. There're also cool shortcuts in bash to avoid using arrow keys:

Ctrl-p - Previous command (same as arrow-up)

Ctrl-n - Next command (same as arrow-down)

Ctrl-j - Sends Newline (same as Enter)

Ctrl-b - Move one char left (same as arrow-left)

Ctrl-f - Move one char right (same as arrow-right)

Alt-b - Move one word left (same as Ctrl-arrow-left)

Alt-f - Move one word right (same as Ctrl-arrow-right)

Ctrl-u - Delete from cursor to beginning of prompt

Ctrl-k - Delete from cursor to end of prompt

Ctrl-w - Delete previous word from cursor

Ctrl-d - Delete current character (same as del-key)

Ctrl-y - Yank / Paste prevously deleted text

Ctrl-a - Jump to start of line

Ctrl-e - Jump to end of line

Ctrl-- - Undo ;)