Bash: reverse i-search and normal i-search

Ctrl+R gives you reverse i-search of bash history, however, once you have selected a command to edit (by hitting ESC) the next search would start from the same line, because it’s incremental by definition.

This is useful but sometimes you wanna start over. You can rewind/fast-forward your search position in history by:

M->    # move to the latest entry
M-<    # move to the earliest entry

You can also do forward incremental search by Ctrl+S. But unfortunately in bash on most distributions Ctrl+S is assigned to “Stop”.

Check what the meta commands are assigned to by

stty -a

If you see ixon, you need to turn it off by

stty -ixon

http://stackoverflow.com/questions/549810/control-r-reverse-i-search-in-cygwin-bash-how-do-you-reset-the-search

'\r' in bash

Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

#!/bin/bash
tput sc
echo -n “Working…”
for i in `seq 1 5000`;
do
echo $i > \dev\null
done
tput el1
tput rc
echo “Finished!”

‘\r’ in bash

Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

#!/bin/bash
tput sc
echo -n “Working…”
for i in `seq 1 5000`;
do
echo $i > \dev\null
done
tput el1
tput rc
echo “Finished!”

Update
There’s actually a simpler way:

for i in `seq 1 5000`; do
    echo $i > /dev/null               # do some processing
    echo -n -e "working $i\x0d"
done