2

I have a sentence that the bash program is asking a user to make an edit to. Let's say the sentence is "The quick brown fox jumps over the lazy dog". Right now, I echo the sentence and then ask the user to respond back with the edited version.

echo "The quick brown fox jumps over the lazy dog"
read -p "Edit: " newSentenceVariable

--> Will show: Edit: (user inputs edited sentence) (user return)

Is it possible to do something where the user's input is already pre-filled with the initial sentence, so that they can make their edits and then hit enter.

read -p "Edit: " "The quick brown fox jumps over the lazy dog" newSentenceVariable

---> Would show: Edit: The quick brown fox jumps over the lazy dog

This way, if there are no edits to be made to the sentence, the user just hits enter. And if they have to add a period to the sentence, they type "." and then the return key. This would all be instead of making the user copy/paste or re-type the whole sentence into the prompt response.

Thanks

echo $BASH_VERSION
3.2.57(1)-release
1
  • 1
    Updating to bash 4 added the -i argument. So yes, this is a duplicate now that I'm on 4. Not sure if there's a solution for 3 or lower. Commented Oct 13, 2018 at 15:47

1 Answer 1

2

bash 4+ solution:

read -e -p "Edit: " -i "The quick brown fox jumps over the lazy dog" newSentenceVariable

From help read:

-e use Readline to obtain the line in an interactive shell
-i text use TEXT as the initial text for Readline

Sign up to request clarification or add additional context in comments.

6 Comments

Checking if the macOS version has anything like this argument.
@MatthewSabatini: Add output of echo $BASH_VERSION to your question.
Tested on 5.2.21(1)-release, no longer works.
@whatthe It works on my 5.2.26(1)-release
For you that it works for: Could you explain what OS you are running. Because I'm fairly certain at this point (having tested in Fedora Workstation 42, latest Ubuntu and randomly Ubuntu 10.04) that this is some custom downstream patch or so.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.