1

I am asking this regarding the answer in "How do I prompt for Yes/No/Cancel input in a Linux shell script?".

The code:

state="CA"
city="Los Angeles"
while true; do
    read -e -p "State: " -i $state state
    read -e -p "City: " -i $city city
    echo "Your state: $state"
    echo "Your city: $city"
done

This is how it runs.

$ ./test.sh
State: CA
City: Los
Your state: CA
Your city: 
State: DE
City: city
Your state: DE
Your city: 

1st input: city not shown up correctly

1st output: I didn't type city, so it should show "Los", right?

2nd input: state changed to "DE", works good which means the shell supports that

but city defaults to "city", again I didn't type

2nd output: city not correct

Anyway, I think the problem is the space in "Los Angeles". How to fix it?

Thanks a lot!

Extra Info

This is the bash that I am using.

$ bash --version
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Answer Found

Should use quote in -i, like

read -e -p "City: " -i "$city" city
Community
  • 1
  • 1
user180574
  • 5,681
  • 13
  • 53
  • 94

2 Answers2

4
read -e -p "City: " -i $city city

$city has spaces so -i is 'Los' instead of 'Angeles'

So Angeles will be the variable set by read

ichramm
  • 6,437
  • 19
  • 30
2

Your belief about the problem is correct; and you have the info about how to fix it right there in front of you....

read -e -p "City: " -i "$city" city
tabstop
  • 1,751
  • 11
  • 10