Live data from Hacker News

Understanding Bash History

symkat.com

1–10 of 28 posts

Re: Understanding Bash History

#6
Here are some of my other favorites:

^foo^bar replaces the foo in the last command with bar

example: less setup.conf, ^less^vim

alt+. recalls the last argument of the last command (in emacs mode, anyway)

example: less setup.conf, vim alt+.

As mentioned, Ctrl+R is great for history searching. Repeatedly pressing Ctrl+R goes to the next result, and you can use the arrow keys to edit.

cd - goes to the last directory. It handles most of the use cases for pushd and popd.

Re: Understanding Bash History

#7

Mentioned in the comments, but another extremely useful feature is if you type ctrl+r and start typing a command, it will give you the last command you ran that contains that substring.

This is the main way I use the history, other than just cycling through the last few commands with the up arrow. I've just never found the bang commands useful enough to be worth remembering for the relatively few times they would be useful. ADDED: Note that I have a pretty large .aliases file that I have profile run when I log in. Any complicated command lines that I run at all frequently get aliased and added to the list. Or for longer lines I convert them to a script in my $HOME/bin.

Re: Understanding Bash History

#8
While what is covered is very well presented, post doesn't mention `shopt -s histappend`, which is one of the most important things to note when dealing with history in bash.

Also useful: you can append :s/foo/bar to bang commands to substitute once, :gs/foo/bar to substitute globally. `man bash` History Expansion section has the other options under subheading 'Modifiers'.

I highly recommend the following resource -- it's what really got me saving keystrokes:

http://www.ukuug.org/events/linux2003/papers/bash_tips/

Re: Understanding Bash History

#9
I used Linux for years without knowing about the bash history file (though I knew about hitting the up arrow). As soon as I discovered this file, I looked up how to set it to save all my history forever without rollover - what a good resource it would be to have all the commands I've ever used!

Anyone know of a utility for saving annotations about the bash history lines? (Date, comments, current directory, etc.?)

Re: Understanding Bash History

#10
Heh. This is funny timing, as .bash_history made me feel like an idiot just yesterday morning.

I was sitting here between queries and I thought 'y'know, all I ever do with history is grep for things. I wonder what else I can do?' When I want to learn about something quick, I usually start with the built-in help; so I did

history --help

The built-in help for history, unfortunately, is woefully inadequate; this was the output:

history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

I said to myself: "Hmm. Wonder what those do?" -- and in a moment of bash stupidity, I thought: "well, let's try it!"

history -c

There's no output from that command, but it didn't take long to figure out what it does. It deletes your entire bash history, clearing the history in RAM and emptying .bash_history.

Since I use history all the time, I panicked. Holy crap! What have I done? I spent a few minutes internalizing the obvious lesson that everybody knows - never run commands without knowing them first. Thankfully, though, I had another bash open at the time. And after I'd beaten myself up over it, I thought for a moment, jumped over to the other shell, and did:

cd && history | cut -c 8- >> .bash_history

... because (after all) the evil, evil history -c command doesn't kill the history from RAM in other shells.

Lucky me.

Post reply on HN