Bash : preserve string with spaces input on command line? -
i'd allow string captured spaces, that:
echo -n "enter description: " read input echo $input
would produce:
> come in description: wonderful description! > wonderful description!
possible?
the main thing worry when refer variable without enclosing in double-quotes, shell word splitting (splits multiple words wherever there's space or other whitespace character), wildcard expansion. solution: utilize double-quotes whenever refer variable (e.g. echo "$input"
).
second, read
trim leading , trailing whitespace (i.e. spaces @ origin and/or end of input). if care this, utilize ifs= read
(this wipes out definition of whitespace, nil gets trimmed). might want utilize read
's -r
("raw") option, doesn't seek interpret backslash @ end of line continuation character.
finally, i'd recommend using read
's -p
alternative supply prompt (instead of echo -n
).
with of these changes, here's script looks like:
ifs= read -r -p "enter description: " input echo "$input"
string bash input
No comments:
Post a Comment