Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

4 Answers 4

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
add comment

In zsh, you have vared:

% foo=wibble
% vared foo
wibble

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
add comment

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 ...
share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.