fancy bash image

Want some bash-foo?!

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

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 192.168.1.101 netmask 255.255.255.0
# New ip syntax:
$ ip addr add 192.168.1.101/24 dev eth0
$ ip a a 192.168.1.101/24 dev eth0
    

Flush interface:


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

Add new default route:


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

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


Netcat
18.12.2016

Simple portscan: nc -vnzw1 192.168.1.1 80

Perform a GET request to target:

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


vi or cd
15.12.2016

Add the following piece of code to your .bashrc


function vc {
CMDLINE=$(history 2 | head -1 | awk -F " " '{print $NF}')
[[ -f $CMDLINE ]] && $EDITOR $CMDLINE
[[ -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
vc
        


Brace Expansion
13.12.2026

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.


Shortcuts
10.12.2026

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 ;)