Terminal bang (!) not Working as Expected

From what I remember the “!” command in bash is to re-execute a command.

One way of doing this is to run the command history, then running the appropriate entry, !2028 to repeat command 2,028 for example.

What I used to do much more commonly was “repeat last command starting with …” to just repeat the last thing I ran. For example !vi would re-run the last command starting with vi.

I just tried this, and it did not work as expected. It ran a different command, not the last one. Has something changed? Is this expected behaviour, or a bug? Why did it run a different command instead of the last one?

Terminal Output:

($) /mnt/Storage/win10-share: vi ~/.mountShares.sh
($) /mnt/Storage/win10-share: !vi
vi .moveApps.sh 
1 Like

I also remember !cmd should run the last as well.

Just done a quick test:
$ echo 1xx
→ 1xx
$ echo 2xx
→ 2xx
$echo 3xx
→ 3xx
→ 3xx


Seems to work fine here. Have you checked the entries in the history to see what the latest entries are? Did you run commands in another bash session in parallel?

1 Like

What will happen when there are multiple terminals running in parallel?

Interesting. I do have multiple terminals running, but I’m not running the same commands on different terminals. I haven’t edited that .moveApps.sh file today, so I don’t know why it would open that one.

!! runs the last command. I often use sudo !! in the case I’ve forgotten to run the command as root.


A quick web search give me this, and lots of other posts: