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
0

2 Answers 2

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

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

1 Comment

If you don't believe try adding echo "Angeles: $Angeles", it will print Angeles: Los
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

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.