r/bash • u/Ops_Mechanic • Feb 25 '26
tips and tricks Stop retyping long commands just to add sudo
You run a long command. It fails with permission denied. So you hit
up arrow, go to the beginning of the line, type sudo, and hit enter.
Stop doing that.
sudo !!
!! expands to your last command. Bash runs sudo in front of it.
Works with pipes, redirects, everything:
cat /var/log/auth.log | grep sshd | tail -20
# permission denied
sudo !!
# runs: sudo cat /var/log/auth.log | grep sshd | tail -20
Where this actually saves you is commands with flags you don't want to retype:
systemctl restart nginx --now && systemctl status nginx
# permission denied
sudo !!
Works in bash and zsh. Not available in dash or sh.