[AuckLUG] Multiline bash history

Ghodmode ghodmode at ghodmode.com
Tue Jun 17 22:42:47 NZST 2008


On Tue, Jun 17, 2008 at 6:42 PM, Joel van Velden <joel at tpnz.co.nz> wrote:
>
> Someone at the last AuckLUG meeting mentioned that they wanted multiline
> bash commands to not bunch up in the history.
>
> try `shopt | grep cmdhist'
>
> `shopt -u cmdhist' turns this off. -s turns back on.
>
> So when on:
> `echo hello; \
> echo world;'
>
> in history appears as:
>  527  echo hello; echo world;
>
>
> And when off:
> `echo hello; \
> echo world;'
>
> in history appears as:
>  523  echo there;
>  524  history

Thanks for looking that up, Joel.  You reminded me of something
related which I wanted to know about and pointed me in the right
direction...

If you type a command on multiple lines, then recall it through the
history or a command line editing hotkey, the newline characters are
replaced with semicolons.  If you would like to have the newlines in
the places you typed them, use

shopt -s lithist

This is especially useful if you are trying to do something complex
from the command line with a loop.  For example the following is
easier to fix when there is an error if the newline characters are not
replaced with semicolons:

ghodmode at home:Documents$ for FILE in *.pdf
> do
>     FILETYPE=`file $FILE`
>     echo $FILETYPE | grep ": PDF.* version 1.5" > /dev/null
>     if [ $? -eq 0 ]; then echo "$FILE is a newer PDF file."; fi
> done
Xen_3.2Datasheet.pdf is a newer PDF file.
xenWhitePaper3.2.pdf is a newer PDF file.

It shows the history like this:

  582  for FILE in *.pdf
do
    FILETYPE=`file $FILE`
    echo $FILETYPE | grep ": PDF.* version 1.5" > /dev/null
    if [ $? -eq 0 ]; then echo "$FILE is a newer PDF file."; fi
done

As an unrelated side-note, that number before the history entry can be
annoying if you want to do a copy and paste.  I couldn't find an
option for the history command that leaves off the numbers, but this
line will do it:

history | sed -e 's/^  *[0-9][0-9]*  *//'

And to give you more information than you really wanted, that pattern
breaks down like this...

Substitute (the beginning of the line, followed by a space, followed
by 0 or more spaces, followed by a number, followed by 0 or more
numbers, followed by a space, followed by 0 or more spaces) with
nothing.

-- Vince



More information about the AuckLUG mailing list