Discussion:
Variable-text embedded in: read -p <quotedText> <arg>
(too old to reply)
nc4NNTP
2019-07-05 15:23:32 UTC
Permalink
PROMPT="Actual prompt"
read -p "This is the prompt:" Go

Pauses for "This is the prompt:" to be user-
read, and
for the variable $Go to be entered and terminated by <Enter>.
But how would I set the <actual prompt> by eg. a preceeding.
<set statement> : PROMPT="Actual prompt" ?

Why does:
read -p $PROMPT Go
read -p "Wait to read:" Go
show "Actual" [ie. the first word only] of $PROMPT ?

Where's a good tutorial about this stuff?

== TIA.
Giovanni
2019-07-05 15:41:29 UTC
Permalink
Post by nc4NNTP
PROMPT="Actual prompt"
read -p "This is the prompt:" Go
Pauses for "This is the prompt:" to be user-
read, and
for the variable $Go to be entered and terminated by <Enter>.
But how would I set the <actual prompt> by eg. a preceeding.
<set statement> : PROMPT="Actual prompt" ?
read -p $PROMPT Go
Put the variable in quotes :

read -p "$PROMPT" Go
Post by nc4NNTP
read -p "Wait to read:" Go
show "Actual" [ie. the first word only] of $PROMPT ?
Where's a good tutorial about this stuff?
== TIA.
Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
< http://giovanni.homelinux.net/ >
Rich
2019-07-05 16:02:34 UTC
Permalink
Post by nc4NNTP
for the variable $Go to be entered and terminated by <Enter>.
But how would I set the <actual prompt> by eg. a preceeding.
<set statement> : PROMPT="Actual prompt" ?
read -p $PROMPT Go
You've already got the answer for here in another posting.

But the explanation for why it failed is that without quoting the
variable reference, read saw this:

read -p Actual prompt Go

And resplit it up into these parameters:
read
-p Actual
prompt
Go

Which is not what you wanted.

With quotes, the splitting into parameters becomes:

read
-p "Actual prompt"
Go

Which was what you wanted.
Post by nc4NNTP
Where's a good tutorial about this stuff?
They are all over, but you do have to go looking for them.

https://www.gnu.org/software/bash/manual/html_node/Quoting.html

https://www.linuxjournal.com/content/bash-quoting

https://bash.cyberciti.biz/guide/Quoting

But a general rule to follow, that will work most of the time, is:

Always put quotes around any variable references.

I.e., generally always do: "$VAR"

Instead of: $VAR

And most problems (until you get into some really advanced Bash usages)
will go away for you.

Loading...