Today I learned another really cool thing bash can do and then I thought it is worth it to start sharing various tips and tricks. Most of them arise from the fact that everything is file in *nix.
Some of the tips are actually borrowed from awesome application called enki.
Situations when disabling command history appending is needed. To temporary disable command logging use:
set +o history
To enable logging again:
set -o history
bash supports vi and emacs modes. The default mode is emacs (all the shortcuts like Ctrl-E or Ctrl-A). To enable vi mode type:
set -o vi
Vi input mode is actually a part of GNU readline, so it can be used virtually everywhere in CLI. To set it, use
# ~/.inputrc set editing-mode vi
There are several device files inside
/dev directory. These are not actual files on disk, rather they provide
an interface to access various useful character data streams. Some of such files:
# Redirect all complains to the black hole modprobe somemodule 2>/dev/null
# Infinite supply of zeros # Remove partition table from the disk dd if=/dev/zero of=/dev/sdX bs=1M count=5
This one is actually pretty amazing
# Check if postgres is alive. # Look Ma, no netcat or nmap! echo -e "HEAD / HTTP/1.0\n" >/dev/tcp/localhost/5432 && echo 'alive!'
Not quite useful in bash itself, but it can be used inside configuration files.
# nginx.conf access_log /dev/stdout;
Bash is suited for text manipulation very well, like most of CLI commands do. Passing text data back and forth becomes trivial operation with pipes.
# Read what 'ps' says and filter strings containing word 'ruby' ps -a | grep ruby
# 'pv' is 'dd' on steroids. 'tee' reads stdin, duplicates it both to $1 and stdout pv <livecd.iso | sudo tee /dev/sdX >/dev/null
# Transfer files though pipe via ssh. Useful application are: # - writing files as non-root user with root permissions cat file | ssh email@example.com "sudo tee /root/file" # - compressing data over ssh tar -cpz mydir/ -O | ssh firstname.lastname@example.org "tar xp"
# A long pipeline ldconfig mybinary | grep '^lib' | uniq | sort | tee libs.txt
That’s it for this time. Since bash is so powerfull, there’s definitely more hacks coming!