How to write a script to sudo the last command [duplicate]

You could just use bash's !!: it is a shortcut to rerun the last command.

sudo !!

Anyway bash offers many shortcuts for anything, you could just press the up arrow (in order to get back the last typed line), then press ^A or Home and add sudo there.

Here a useful bash cheatsheet