help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Input history when using read


From: Matthew Cengia
Subject: Re: [Help-bash] Input history when using read
Date: Tue, 5 Feb 2013 23:36:08 +1100
User-agent: Mutt/1.5.20 (2009-06-14)

On 2013-02-05 13:00, Suvayu Ali wrote:
> Hi,
> 
> I have a script that searches through emails for me.  I read in my
> search query either as positional parameters or read.  Often I want to
> repeat a variation of a complicated search.  I was wondering if it is
> possible to have some sort of history when I get the query using read.
> This is what I have so far:
> 

Try this:

  address@hidden:tmp$ cat muttsearch
  #!/bin/bash -ueE
  # Allocate new history file and delete it on exit.
  export HISTFILE=~/.mutt_search_history
  if [[ ! -e $HISTFILE ]]; then
    touch "$HISTFILE"
  fi

  history -c                      # Clear all current history.
  history -n                      # Read everything from history file.
  read -ep "Search query: " query
  echo "$query" >> "$HISTFILE"
  address@hidden:tmp$

It makes use of existing readline features, allowing you to store stuff
in history, and search it with up/down arrows, as well as Ctrl+P/N/R/I.
I'm not sure whether there's a better way to append to the history file
than the echo above; hopefully somebody else can improve on that.

-- 
Regards,
Matthew Cengia

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]