How to break a long string into multiple lines in the prompt of read -p within the source code?
32
votes
4
answers
78637
views
I am writing an installation script that will be run as
/bin/sh
.
There is a line prompting for a file:
read -p "goat may try to change directory if cd fails to do so. Would you like to add this feature? [Y|n] " REPLY
I would like to break this long line into many lines so that none of them exceed 80 characters. I'm talking about **the lines within the source code** of the script; *not* about the lines that are to be actually printed on the screen when the script is executed!
What I've tried:
- Frist approach:
read -p "oat may try to change directory if cd fails to do so. " \
"Would you like to add this feature? [Y|n] " REPLY
This doesn't work since it doesn't print Would you like to add this feature? [Y|n]
.
- Second approach:
echo "oat may try to change directory if cd fails to do so. " \
"Would you like to add this feature? [Y|n] "
read REPLY
Doesn't work as well. It prints a newline after the prompt. Adding -n
option to echo
doesn't help: it just prints:
-n goat oat may try to change directory if cd fails to do so. Would you like to add this feature? [Y|n]
# empty line here
- ###My current workaround is
printf '%s %s ' \
"oat may try to change directory if cd fails to do so." \
"Would you like to add this feature? [Y|n] "
read REPLY
###and I wonder if there is a better way.
Remember that I am looking for a /bin/sh
compatible solution.
Asked by Mateusz Piotrowski
(4983 rep)
Mar 26, 2016, 08:37 PM
Last activity: Jan 6, 2021, 10:19 PM
Last activity: Jan 6, 2021, 10:19 PM