1

I was trying to read user input twice on the same line from a bash script. It's not very useful at all, but it's pretty and I'm trying to achieve this.

The relevant part of my code looks like that:

#!/usr/bin/env bash

FROM_YEAR=2015
TO_YEAR=2017
read -e -p "Backup from " -i "${FROM_YEAR}" FROM_YEAR
read -e -p " to "         -i "${TO_YEAR}"   TO_YEAR

With this code I get the following:

Backup from 2015 *press enter*
 to 2017

But I would like output like this:

Backup from 2015 *press enter* to 2017

Of course, I want an output without the marking (*press enter* ).


How can I achieve that with bash script?

Thank you :)

3
  • Pressing Enter to answer a prompt automatically goes to the next line. bash is not a form-filling application. Commented Feb 2, 2017 at 1:29
  • There's no way to workaround this? Commented Feb 2, 2017 at 1:33
  • 2
    Use a tool for full-screen input, instead of the read built-in. Commented Feb 2, 2017 at 1:40

3 Answers 3

1

There are always workarounds, but they are not always portable. There's a good chance this will work:

#!/usr/bin/bash

FROM_YEAR=2015
TO_YEAR=2017
read -e -p "Backup from " -i "${FROM_YEAR}" FROM_YEAR 
printf '\033[1A\033[16C'
read -e -p " to " -i "${TO_YEAR}" TO_YEAR

Note that this is a terrible hack, and if FROM_YEAR does not have exactly 4 digits after you edit it, the result will not be pretty.

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

Comments

0
FROM_YEAR=2015 TO_YEAR=2017
read -p "Backup from ${FROM_YEAR} to ${TO_YEAR}: " FROM_YEAR TO_YEAR
echo $FROM_YEAR $TO_YEAR 

Output, if the user enters "3 4":

Backup from 2015 to 2017: 3 4
3 4

Comments

0

You could, I guess, clear the screen before the initial display, then clear the screen again after the user presses [Enter] so that you can re-display things where you want them.

I have bash scripts with menus that work like that. You have to re-display everything, but for simple needs it does the job (if you do not mind clearing the screen, of course).

Comments

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.