Another starter article to grow with my needs, we’ll start off with the shebang, or bang…or simply “!”. The first two commands I believe are the most useful and could be commonly used. Note that some of these can be a bit difficult to remember, we’ll also go over making aliases so we can use something a little easier to remember in a later article.
!:p
Prints the last command executed (does not run it)
!!
This bang command, when entered into the bash shell will run the previously entered command. It the same thing as hitting the up arrow to take you to the previous command and then hitting enter.
>date
Monday, June 6, 2011 3:56:20 PM CDT
>!!
date
Monday, June 6, 2011 3:56:22 PM CDT
Whats nice about the !! command though is if you forget to run something as root you can run the same command again, slighly modified:
>mount -a
mount: only root can do that
>sudo !!
(Success)
!<command>
(<command> being whatever command you want to reference).
This will run the last command that started with <command>. If you ran ‘ls -al ~’ a few commands ago and then you type ‘!ls the full command will be run again.
>ls
total 0
-rw-r----- 1 scottro root 0 2011-06-06 16:01 file.txt
>date
Monday, June 6, 2011 16:02:20 PM CDT
>!ls
ls
total 0
-rw-r----- 1 scottro root 0 2011-06-06 16:01 file.txt
!<command>:p
This will display the last instance of the command instead of running it.
>ls ~/test
total 0
-rw-r----- 1 scottro root 0 2011-06-06 16:06 file.txt>!ls:p
ls ~/test
!$
References the last ‘word’ of the last command.
For example, create a file then emacs the same file
>touch /var/tmp/file.txt>emacs !$
!$:p
Prints the last word of the last command
!*
Substitution for the first word of the last command
>which date
/usr/local/bin/date>type -a !*
type -a date
date is /usr/local/bin/date
date is /usr/bin/date
date is /usr/bin/date
date is /bin/date
!*:p
This will print the previous command without the first word.
Other references:
~
(tilde)
This references your home directory.
scottro@sudobash.net /var/tmp> cd ~
scottro@sudobash.net /home/scottro>