2

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.

4 Answers 4

7

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

read -e foo
1

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 ...
1

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.

0

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

1
  • and for a prompt, try replacing the echo with echo -en $prompt$input${input//?/\\b}; read reply;
    – user41571
    Apr 27, 2010 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .