[Bash] Rerun Bash Commands with History Expansions (!! & !$)

!! & !&

History expansions let you interact with bash's history. For example, if you forgot to run a command with sudo, you can sudo !! to rerun the last command with it. !$ can be used to access the last argument of the last command.

Rerun last command

date
## print the date
!!
## print the date again

It is useful when you forgot to add sudo

ifconfig en0 down
## error because of sudo 
sudo !!

Last argument of previous command

touch script.js
## last argument is 'script.js'
chmod +x !$
## the same as chmod +x script.js
posted @ 2021-02-11 20:02  Zhentiw  阅读(427)  评论(0)    收藏  举报