To search for a command in the history press ctrl+r
multiple times ;-)
You can also grep through the history using:
history | grep YOUR_STRING
I would have said that it is simply Tail Call Optimization, but in fact (as the last link points out), bash
doesn't optimize tail calls. It only seems to optimize the case where the command to be executed is a simple command (that is, not a compound command).
The second command is not a tail call of pstree
, because pstree
is not the last thing in the command line. (It's a tail call of echo
, but echo
is usually a built-in so no subprocess will be created for it regardless.)
To save reading all those links (although I hope they are interesting), the idea is that if you know that a function/program/whatever will return immediately after calling some other function/program/whatever, and that the value returned will be the value returned by the called function/program/whatever, then you might as well just reuse the current stack frame (or process, in the case of a shell script) instead of pushing a new stack frame (creating a new process), calling the function (running the script), and then returning. In a shell script, you can do that manually by using exec
for the last command, but it would be possible for the shell to do that automatically.
zsh
and ksh
both seem to be able to do that, but not bash
:
$ zsh -c 'if [[ -n foo ]]; then pstree -s $$; else echo hello; fi'
init───lightdm───lightdm───init───konsole───bash───pstree
$ ksh -c 'if [[ -n foo ]]; then pstree -s $$; else echo hello; fi'
init───lightdm───lightdm───init───konsole───bash───pstree
$ bash -c 'if [[ -n foo ]]; then pstree -s $$; else echo hello; fi'
init───lightdm───lightdm───init───konsole───bash───bash───pstree
But that observation is just an observation, based on the versions of those shells which I happen to have installed, so YMMV:
$ zsh --version
zsh 5.0.2 (x86_64-pc-linux-gnu)
$ ksh --version
version sh (AT&T Research) 93u+ 2012-08-01
$ bash --version
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
Best Answer
Your tty probably has the
ixon
andixany
bits set.ixon
enables XON/XOFF flow-control, so when you hit CtrlS, the tty device stops output. Butixany
means that any other key will restart output, so most people don't notice it ever stopped.Some people find flow-control useful, but you can disable it by running:
and then CtrlS should work as expected for searching in bash.
You might want to add that to your
~/.bashrc
!