simple command expansion in bash

2020-06-02

 | 

~1 min read

 | 

114 words

In the Terraform tutorials, the authors make use of a pattern I’d not seen before:

$ mkdir new-directory && cd $_

The end result is a new directory is created, and as might be expected, the directory is changed to the new-directory.

How it works, however, is interesting.

Per @PSkocik on a Stack Overflow conversation on the topic:

$_ expands to the last argument to the previous simple command or to previous command if it had no arguments.

For more on Command Expansion, see the Bash manual.

Otherwise, this post on parameter and command expansion from compciv.org is quite helpful for seeing other ways expansion can be used to great effect.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!