Cryptic but consistent
(832) 422-8646
Contact
Suppose you’ve never worked at the command line and you’re reading a book about the bash shell. You read that !$ is a shortcut to refer to the last word of the previous command. That little fact will almost certainly not stick in your head for a couple reasons. First, you probably see no need for such a shortcut. Second, the syntax seems completely arbitrary.
But then after you use the command line a while, you might begin to notice a pattern. You often have to run a pair of commands with the same last argument, such as when you make a directory then navigate into it.
mkdir /some/long/path/name<br>cd /some/long/path/name
Maybe someone sees you typing this and offers a helpful suggestion: you can type !$ to repeat the last part of the previous command.
mkdir /some/long/path/name<br>cd !$
Once you learn this, you’ll see uses for it regularly. Sorta like when you buy a green Toyota Corolla and suddenly become aware how many green Toyota Corollas there are out there.
So then you start to use !$, but you have some doubts: is the syntax !$ or $!? As long as it’s just two cryptic symbols, you may have a hard time remembering.
Although the command line is cryptic, it is also consistent. On the command line, and in Unix [1] culture more generally, ! refers to a command. For example, you can run a command from inside the Vim text editor with the :! command. And $ often refers to the end of something, such as the end of a string in a regex. So you type !, telling the shell that you’re about to refer to part of a string, then type $ to specify that last part.
The characters !$ are an example of an event designator , a sort of mini language for referring to parts of commands. The syntax may seem mysterious, but every bit of it is analogous to syntax elsewhere in Unix culture. For example,
!:s/foo/bar/<br>replaces the first instance of foo with bar, and
!:gs/foo/bar/<br>will replace all instances. This is very similar to commands in sed and in vi For more on event designators, see this recent article by Filip Roséen.
There’s a saying that when the student is ready, the teacher will appear [2]. A less woo-woo version of this saying would be that until the student is ready, it doesn’t matter whether a teacher is there. Once you’ve used the command line to see a need for various shortcuts, there’s a much better chance they’ll stick.
Related posts
Just-in-case versus Just-in-time
Brace expansion
Shell variable ~-
[1] Linux has so thoroughly eclipsed Unix that many people say "Linux" when they’re referring to things inherited from Unix that are now part of Linux and other operating systems, as well as various software packages and programming languages.
Here’s a plot of references to Unix versus Linux over time, via Google’s ngram viewer.
[2] When I first heard this, someone told me it was an Asian proverb. It sure sounds like one. But apparently it comes from a 19th century pastiche of eastern and western ideas.
Leave a Reply<br>Your email address will not be published. Required fields are marked *<br>Comment *<br>Name *
Email *
Website
Search for:
John D. Cook, PhD
My colleagues and I have decades of consulting experience helping companies solve complex problems involving data privacy, applied math, and statistics.
Let’s talk. We look forward to exploring the opportunity to help your company too.