Posted: . At: 8:27 AM. This was 5 years ago. Post ID: 12846
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Another way to print the last used command in your bash shell.


This very simple trick, prints the last used command in your bash history, with one weird trick.

deusexmachina:~ jason$ echo !!
echo ifconfig | awk '/inet / { print $2 }' | sed -n 2p

To print the shell you are using, this will work very well.

4.4 Mon Jan 14 jason@Yog-Sothoth 0: $ echo $0
/bin/bash

This trick will print the inbuilt set flags for the bash shell.

$ echo $-
himBHs

This is a guide to what they mean.


h: Remember the location of commands as they are looked up for execution. This is enabled by default.
i: interactive
m: Monitor mode. Job control is enabled
B: The shell performs brace expansion (see Brace Expansion above). This is on by default
H: Enable ! style history substitution. This option is on by default when the shell is interactive.

Another very neat Linux trick, I ran ps aux in a terminal, then I re-ran it by getting the command line parameters from the bash history.

4.4 Mon Jan 14 jason@Yog-Sothoth 0: $ ps `echo !!:$`

Not sure how useful this is, but it does work and may be interesting to someone out there.

Another useful trick if a user forgets to use sudo before a very long command, just do this instead.

4.4 Mon Jan 14 jason@Yog-Sothoth 0: $ sudo !!

This will rerun the command line with the sudo prefixed to it.


1 thought on “Another way to print the last used command in your bash shell.”

  1. Bonjour,

    Je sais que vous recevez beaucoup de propositions publicitaires ou sans rapport avec votre contenu, alors je vais aller droit au but : j’aimerais beaucoup vous soumettre un article en tant qu’invité ou sponsorisé pour qu’il soit publié sur votre site.

    Cet article est en rapport avec le contenu de votre site web, et bien sûr, il est utile et informatif, ce que votre audience pourrait apprécier.

    Si vous acceptez les articles extérieurs ou sponsorisés, j’aimerai connaitre le prix pour pouvoir publier sur votre site.

    En vous remerciant par avance pour votre temps et l’attention que vous porterez à ma demande!

    Sincères amitiés.
    Futing

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.