Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a simple script to grep my bash history to find a particular string. On the command line, I can do the following, which works fine:

history | grep git

However, when I create a bash script with the same command as above, suddenly history returns nothing:

history | grep git

When I remove the first line #!/bin/bash, my script works again. What is happening here? How can I use history inside a bash script?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

The following is for bash 4.0 or higher only, but it does the trick. :)

#!/bin/bash -i
history | grep sometext

Works! Try it out. (-i = interactive, a flag not available to bash 3.x and earlier)

share|improve this answer
works indeed. Thanks a lot. –  Martin Vegter Dec 9 '14 at 23:59
Thanks too to the dude who recently downvoted me just because he can. (Probably a bash v3 user, out of mere frustration, haha). At least for Martin my solution was of great help, that's (at least) a comfort. –  syntaxerror Feb 8 at 11:17

When you run a bash shell script, for example by running an executable file whose shebang line is #!/bin/bash, the script is run in a non-interactive shell. One of the features of a non-interactive shell is that history is disabled. So if you want your script to be able to use history, you'd need to do something like this:

HISTFILE=~/.bash_history   # Or wherever you bash history file lives
set -o history             # enable history
history | grep git

Of course, in this case you could have just done:

grep git ~/.bash_history

Since both of the above require that the history file (~/.bash_history) be up to date, which it almost certainly will not be.

I'm not sure how you run your script after the shebang line is removed. Sourcing the file (eg., with .) is not the same as running the script because the commands run in the current shell, which does have history enabled.

share|improve this answer
What about doing history -a right after the set command to make sure the last commands are appended to the file first? –  Riguefort Ultraquaillette Feb 3 '14 at 22:16
@illuminÉ: won't help. The shell started to run the script is a completely diffetent process; it starts with a frsh empty history. –  rici Feb 3 '14 at 22:26

If you are using bash, then I usually set up the following two aliases in my ~/.bashrc

alias h='history'
alias hh='history | grep $*'
share|improve this answer
This does not actually answer the question which was about how to run history | grep foo from within a script where, by default, neither the history command nor your aliases will be available. –  terdon Feb 4 '14 at 1:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.