Live data from Hacker News

Understanding Bash History

symkat.com

21–28 of 28 posts

Re: Understanding Bash History

#21
post #3

I learned a lot from this article. It is very useful for novices such as myself. My uparrow key thanks you for writing this.

If you're hitting the up arrow more than a few times, you could easily see search your history instead with "history | grep somestring", or, as someone else mentioned, Ctrl+R and start typing a command.

Re: Understanding Bash History

#22
post #20

I use history a fair amount, and I've noticed an issue that none of these articles mention. For history to be useful, you need to be able to reuse previous commands without giving them much thought; in the time it takes to think in detail about a command, you usually could have typed a new one without using history. As a result, commands that are reused via the history mechanism need to be "safe", with no hidden pitf…

find ... !! -delete

Ah, didn't know about that one. Thanx.

(But it still doesn't address the main issue.)

Re: Understanding Bash History

#23
Since everyone's sharing their bash history tips here, I thought I'd throw one out. I've taken to tossing the following lines in my root user's .bashrc over the last few years (which I adapted from a colleague years ago):

  [ ! -d $HOME/.history ] && mkdir $HOME/.history
  set -- `/usr/bin/who -m`
  HISTFILE="$HOME/.history/`/bin/date +%Y-%m-%d.%T`.`/bin/hostname -s`.$$.$1"
You end up with a filename something like:

  2010-09-17.13:53:27.foohost.14379.logic
The idea is that each shell session gets it's own history file, and the files are kept "indefinitely"; ie. whenever I manually get around to cleaning them up. Keeping that history around is cheap (we're talking about a few megabytes over the life of a given system), and can be invaluable in figuring out the how and why of a particular change even months later.

It also serves the purpose of making a best-effort attempt at gathering who "owned" that session (ie. who su'd to root); with multiple admins, this can be pretty handy. It's easily bypassed (and console logins will just show up as "root"), so using this as any kind of audit system would be ridiculous; it's just a useful way of going back in time to see who did what (usually looking of work I did myself, to see how/why I might have made a particular change months ago).

The one downside: you lose cross-session history. In practice, especially with multiple users accessing a shared account (multiple admins, etc), this hasn't really bothered me much, as getting someone else's history is annoying anyway. But, some people lean pretty hard on their bash history, and I definitely wouldn't do this for a personal account. (There, I'd just do "shopt -s histappend".)

Re: Understanding Bash History

#24

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…

As far as these little shorthands go, you've left out the one I use most all day long; the ever-handy !:-, which means 'the last command minus the final argument': $ cat ~/my_huge_dataset.csv | tr '\|' ',' >> testing.csv $ !:- for_real_this_time.csv >> cat ~/my_huge_dataset.csv | tr '\|' ',' >> for_real_this_time.csv $ This is really, really handy for me - particularly in two situations: first, when (like above) I'm…

I like the !:-, even though it's super ugly. It's the perfect complement to alt+. !! is a classic, but the article mentions it.

Re: Understanding Bash History

#25

I learnt something handy in BASH recently - operate-and-get-next (Ctrl-o). If you type: $ echo one one $ echo two two $ echo three three Then up-arrow back to 'echo one'. Then press Ctrl-o instead of enter it will execute the command and display the following one in your history ('echo two' in this case). Very handy for replaying a series of commands. http://www.faqs.org/docs/bashman/bashref_101.html

This is in the man page for bash on Snow Leopard, but it doesn't seem to actually work. Does anyone know why?

Re: Understanding Bash History

#26
post #25

I learnt something handy in BASH recently - operate-and-get-next (Ctrl-o). If you type: $ echo one one $ echo two two $ echo three three Then up-arrow back to 'echo one'. Then press Ctrl-o instead of enter it will execute the command and display the following one in your history ('echo two' in this case). Very handy for replaying a series of commands. http://www.faqs.org/docs/bashman/bashref_101.html

This is in the man page for bash on Snow Leopard, but it doesn't seem to actually work. Does anyone know why?

There's a solution here:

http://hintsforums.macworld.com/archive/index.php/t-82501.ht...

'stty -iexten' fixed it for me on my Mac.

Re: Understanding Bash History

#28
post #27

Earlier quoted context omitted.

Ah, didn't know about that one. Thanx. (But it still doesn't address the main issue.)

HISTCONTROL="ignorespace" now you can keep a given command from history find ... !! -delete

That's a nice feature.

Now it remains to be seen whether I can train myself to always precede "unsafe" find commands with a blank.

Post reply on HN