Tips For Using Bash History
Here are a few ways to use the bash history efficiently. It can make your (linux) life easier, but you should be careful when using some of them:
1. history: this command will show you the last commands you’ve typed, notice that each command (line) has a number, we’ll make use of it in the next tip.
2. exclamation mark + number: typing an exclamation mark followed by a number (e.g. !102) will call (i.e. re-enter) the command at line 102 (this is the number of the command as appears when you invoke the “history” command.
3. bang bang: A pair of exclamation marks (i.e. !!) will be substituted by the last command you ran. This can be helpful for example if you try to type a long command as a user, and it refuses to run because it needs super-user privilleges, then you can simply type:
sudo !!
This will run the previous command with super-user privilleges.
4. exclamation mark + string: typing an exclamation mark followed by a string (e.g. !ec) will run the last command in your history that begins with this string (be careful when using this). For example, suppose you ran this sequence of commands:
echo hello
grep blabla somefile.txt
Now if you run this:
!ec
This will run “echo hello”
5. exclamation mark + question mark + string: I’m writing about this option for your information only, I don’t recommend using it since it can surprise you. If you decide do use it, be very careful:
typing this:
!?ep
Will run the last command in your history which includes the string “ep” anywhere in the command.
6. Dollar sign: suppose the last command you typed was something like this:
command arg1 arg2 arg3
The variable $_ will restore the last argument (i.e. arg3), so you can use it now:
anothercommand $_
and this will be equivallent to typing
anothercommand arg3
Another option is using !$ (exclamation mark followed by a dollar sign), instead of $_ . This is almost the same, except that this will also print the command after “translation” to the shell before executing it.
7. Go through the history of last arguments: This is a shortcut: typing
1 | [alt] + . |
(this is: alt and a dot), will insert the last argument of the last command. Typing it twice will insert the last argument of the command before the last instead, etc.. For example, suppose you typed this sequence of commands:
ls file1
cat file2
Now, if you type this :
1 rm [alt]+.
(again:
1 | [alt] + . |
means the alt key and the dot key)
this will actually remove file2.
And if you type this instead:
1 rm [alt]+. [alt]+.
This will remove file1.
Note: You can use
1 | [esc]+. |
instead, but I find the the alt key much more comfortable to use in this case.
Credits: Command Line Warriors
Credits: shell-fu
If you enjoyed this post, make sure you subscribe to my RSS feed!Related posts:
Leave a Reply
About Me
Tags
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- iPhone
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Regular Expressions
- Science
- Security
- Shell Scripts
- Short Posts
- Social Networks
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Calendar
Archives
- September 2010 (1)
- August 2010 (2)
- July 2010 (5)
- June 2010 (1)
- May 2010 (1)
- April 2010 (3)
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)
