Bash Gibberish - Type Less, Do More

This post is a few years old now, so some details (or my opinions) might be out of date.
I would still love to hear your feedback in the comments below. Enjoy!

Over my several years of experience with bash I found several really useful tips and tricks. This post will deal with some of the more obfuscated looking “variables” that Bash provides.

Comic courtesy of

$?: Check the status of the last command

$ hg branch
abort: no repository found in '<location>' (.hg not found)!
$ echo $?
255  <---- The "hg branch" command failed!
$ echo $?
0    <---- The "echo" command succeeded!

!!: Repeat the last command entirely

$ cat secret-file 
cat: secret-file: Permission denied
$ sudo !!
sudo cat secret-file 

I specifically love to use this with sudo because it looks like you’re yelling at your computer and it caves.

!$: Repeat the last parameter of the last command

While this seems more niche and less useful than the previous point, I actually use this the most. It’s useful when you want to run two command on the same file, which is very common.

$ mkdir -p /tmp/a/really/complex/path/you/dont/want/to/repeat
$ cd !$
cd /tmp/a/really/complex/path/you/dont/want/to/repeat

$$: Get your process id

$ echo $$
$ ps
  PID TTY          TIME CMD
 2122 pts/1    00:00:00 bash
 2632 pts/1    00:00:00 ps
Discuss this post at the comment section below.
Follow me on Twitter , Facebook or Google+

Similar Posts