My bash usage in 7 tricks
Somebody on IRC convinced me to try ZSH, then maybe this is the last chance to document my bash tricks. So here it goes:
Sometimes you want to reuse the last argument of the last command and there is a shell variable that holds exactly this. Be careful to quote it to avoid issuing multiple arguments instead of one. You can argue that it’s easier to navigate through the history of commands, but if you use something like HISTCONTROL=’ignorespace’, the history is not always available. I’ll use the history of commands and some handful Emacs hotkeys most of the time too.
It’s not unusual to issue arguments that have common parts and brace expansion is here to help us.
If you’re unsure about the effects of the expression, just put “echo ” in front of everything and you’ll have a “preview” of what the command would do.
This trick saved me from sooo much typing.
It combines history navigation and data filtering. Simple and fast.
My system’s locale is not english and this wouldn’t mean too much, but when the time to communicate with other people comes, I need a fast way to reproduce the problem using the developer’s standard language. Want to report a bug? Use the original error messages. Want to know why you are getting a warning? Search engines will help less using localized messages.
$(), “$()” and ` `
Use the output of a command as argument (or arguments) for another command. I don’t use this trick on my day-to-day use of bash, but it’ll be probably used when the time to write scripts come.
pipes, pipes, pipes everywhere
It’s the Unix way. One command that only does one thing it does it well.
It is useful in so many ways:
- Process the input set (eg. how many tests does project X implement).
- Filter a large data set (eg. which mount points are read-only?).
- Interactively filter a data set (eg. which files have missing copyright notices?).
- Add a nice pager to the output of a process.
- Follow the output of a command and log it at the same time (eg. tee).
sleep 8h && ponymix set-volume 100% && while true; do mplayer Música/Disturbing_music.ogg; sleep 5; done
This is my alarm clock. The behaviour of my smartphone’s alarm clock is pure shit.
If you hit Ctrl+C, sleep will return false, breaking the control flow and aborting the alarm.
The music won’t stop until somebody unlock the screenlock, access the terminal and hit Ctrl+C. The audio hardware is loud enough and eventually I’ll wake up.
And my top 7 commands
- sudo: Run commands as root (it does more tricks than su).
- yaourt: Wrapper around pacman that can search for packages on AUR.
- git: I type git a lot.
- nano: A simple text editor. It’ll open fast and close in no time. Useful for simple editing tasks.
- cd: I type cd a lot.
- ls: I also type ls a lot.
- ssh/scp: This is the tool you’ll use when you have more than one machine/system under your command.
The above list was created from the history stored on my netbook, but I’m sure grep would be in this list if I have used the history stored on my desktop pc.