[phasnip@mijpnb1 Linux]$ history 5 22 10:58 cd ~ 23 10:58 setenv MYVAR hello 24 10:58 echo $MYVAR 25 10:59 cd Teaching/Linux/ 26 10:59 history 5To repeat the last command, use !!.
[phasnip@mijpnb1 Linux]$ !! history 5 23 10:58 setenv MYVAR hello 24 10:58 echo $MYVAR 25 10:59 cd Teaching/Linux/ 26 10:59 history 5 27 11:00 history 5Notice that the very first line of output tells you what `!!' was expanded to.
We can also run any of the commands in the history in one of two ways. The first is to use a single ! followed by the number of the command
[phasnip@mijpnb1 Linux]$ !27 history 5 24 10:58 echo $MYVAR 25 10:59 cd Teaching/Linux/ 26 10:59 history 5 27 11:00 history 5 28 11:01 history 5
The second is to use a single ! followed by some text. The shell we execute the last command that started with this text.
[phasnip@mijpnb1 Linux]$ !echo echo $MYVAR hello
if we type anything after the first bit of text it will be appended to the expanded command.
[phasnip@mijpnb1 Linux]$ !echo everyone echo $MYVAR everyone hello everyone