1

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.

6

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

read -e foo
|improve this answer|||||
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.

|improve this answer|||||
0

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

|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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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