Shell text substitution
• 111 words • 1 min • updated
⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
I learned a neat shell trick this week. In both bash and zsh you can use the
circumflex / caret (^) symbol to find & replace a word from the previous
command.
Usage: ^prev^next. It’s best illustrated with examples:
terraform #
Instead of running:
shell
terraform init -var-file /path/to/foo.tfvars
terraform plan -var-file /path/to/foo.tfvars
terraform apply -var-file /path/to/foo.tfvarsRun:
shell
terraform init -var-file /path/to/foo.tfvars
^init^plan
^plan^applysystemd #
Instead of running:
shell
sudo systemctl restart nginx
sudo systemctl status nginxRun:
shell
sudo systemctl restart nginx
^restart^statusone observation #
zsh will run the substitution right away, whereas bash will allow you
to review and edit the replaced command before running it. ∎
Connections
Shared tags