5

I would like to ask for user input with an editable default value, on Bash 3.x. Is that possible?

In Bash 4, below works, but here -i switch is not implemented.

name="Alex"
read -e -i "$name" -p "Please enter your name: " input
name="${input:-$name}"

3 Answers 3

1

Not exactly but you can do something like:

name="Alex"
read -e -p "Please enter your name [$name]: " input
name="${input:-$name}"

User will not be able to edit the name but still see it as default value. Not exactly what you wanted. I guess you maybe already thought about that..

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

2 Comments

Thanks, but I'm really up to an inline editable default value. Because in the real life example, the default value is not Alex, its longer and more complex.
@noway, I think you need to use some richer language, maybe access readline directly. Perhaps perl, but depends on platforms you are working with.
1

I'm going to guess that the reason you're using a 12+ year old version of Bash is that you must be trying to get this to work in modern OSX, which for some reason still ships Bash 3.2.57. For reference, Ubuntu 10.04 (released 2010-04-29) had Bash 4.1.5(1) cite.

The answer is probably going to be that -- for a lot of shell features -- your Bash version is simply too old to support this. You'll have to either rely on an external program, a workaround, or be content with what the old version supports.

Your Options

Note: I added -r to read (i.e. don't treat \ specially) because shellcheck complains about this.

  1. Use #!/usr/bin/env bash as your script's bangline to bypass the default OSX shell (requires homebrew).

  2. Do without it:

name=Alex
read -r -e -p "Please enter your name [$name]: " input
name=${input:-$name}
  1. Test Bash version before using -i:
name=Alex
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
    # We have a version of Bash that supports "read -i"
    read -r -e -i "$name" -p 'Please enter your name: ' input
else
    # Older version of Bash
    read -r -e -p "Please enter your name [$name]: " input
fi
# Proceed as normal
name=${input:-$name}

Comments

0

In BASH <4 you can do this:

name="Alex"
read -p "Please enter your name: " input && [[ -z "$input" ]] && input="$name"

This basically check after read if input var is empty then sets it to $name.

4 Comments

Thanks, but I'm really up to an inline editable default value. Because in the real life example, the default value is not Alex, its longer and more complex.
Default value can be anything. I just copied your example for demo purpose.
I mean, the users should be able to edit the default value inline, as -i switch provides. Assume that the default value is SomeLong456.674.234.657 but the user wants to input SomeLong456.674.234.658, then he will need to type it all from the start (or copy/paste), but I want to make them be able to just press Backspace and 8.
That will require you to upgrade to BASH 4 since it is very difficult to get using BASH <4.

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.