How to rerun last command in Zsh shell

If you want to execute the last command that you ran in your Zsh shell, the easiest way would be by pressing the up arrow key on your keyboard, the last command is pulled up your on your prompt and you press enter. But do you know there is another way in which you can run the previous command using a command? Simply type to exclamation marks !! - this will execute the last executed command on your Zsh shell. Let's see some examples,

Zsh command to execute previous command
Zsh command to execute previous command

As you can see, when I typed !! the pervious command that was clear was executed again. In the next example it was the who command. Also notice that when you run the double exclamation command it displays the command as text in the first line outputted and then the result of the command,

code2care@mac ~ % !!
ls -ltrh
total 0
drwxr-xr-x+  4 code2care  staff   128B Jan  2 23:03 Public
drwx------+  4 code2care  staff   128B Jan  2 23:28 Pictures
drwxr-xr-x   4 code2care  staff   128B Jan 17 16:16 ClassRoom
drwx------@ 75 code2care  staff   2.3K Feb 13 16:04 Library
drwx------@  3 code2care  staff    96B Feb 13 16:04 Applications
drwxr-xr-x   5 code2care  staff   160B Feb 15 22:32 IdeaProjects
drwx------@ 13 code2care  staff   416B Mar  6 18:01 Documents
drwx------+  7 code2care  staff   224B Mar 13 22:42 Music
drwx------   6 code2care  staff   192B Mar 20 16:54 Movies
drwx------+ 18 code2care  staff   576B Apr  3 12:41 Downloads
drwx------@  8 code2care  staff   256B Apr  3 13:27 Desktop
code2care@mac ~ % 
Try Out Code2care Dev Tools:


Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR -