If you don't mind using perl, you can use Term::Readline
There is also a readline() implementation in C; and probably in other languages. I don't know bash's interface for readline
perldoc Term::Readline
reproduced below, note sample script
> Term::ReadLine(3) User Contributed
> Perl Documentation
> Term::ReadLine(3)
>
>
>
> NAME
> Term::ReadLine - Perl interface to various "readline" packages. If no
> real package is found, substitutes stubs instead of basic
> functions.
>
> SYNOPSIS
> use Term::ReadLine;
> my $term = new Term::ReadLine 'Simple Perl calc';
> my $prompt = "Enter your arithmetic expression: ";
> my $OUT = $term->OUT || \*STDOUT;
> while ( defined ($_ = $term->readline($prompt)) ) {
> my $res = eval($_);
> warn $@ if $@;
> print $OUT $res, "\n" unless $@;
> $term->addhistory($_) if /\S/;
> }
>
> DESCRIPTION ...