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