Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a bash script that would be nicer if when I prompt the user, rather than just asking for input, if it provided a line that the user could edit (but a full text editor would be overkill, it's only one line)

What tool provides this?

dialog's inputbox is almost right, but I'd rather it didn't paint the whole screen.

share|improve this question
up vote 6 down vote accepted

The "read" built-in can do editing, using the Readline library, if you give it the "-e" flag:

read -e foo
share|improve this answer

In zsh, you have vared:

% foo=wibble
% vared foo

and the cursor is then at the end of the wibble, you can edit as you like and when you press enter, you've defined the new value of foo.

share|improve this answer

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)
>        Term::ReadLine - Perl interface to various "readline" packages.  If no
>        real package is found, substitutes stubs instead of basic
> functions.
>          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/;
>          }
share|improve this answer

in bash,

input="Edit this"

echo -en "$input\r"; read reply;

echo $reply${input:${#reply}}

notice the \r (carriage return) in the echo. The bracketee voodoo in the last line prints the reply plus the leftover part of the input that you did not erase.

its not ideal - you basicly overwrite the input var, you dont really edit it.

$2c, *-pike

share|improve this answer
and for a prompt, try replacing the echo with echo -en $prompt$input${input//?/\\b}; read reply; – user41571 Apr 27 '10 at 10:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.