Take Multi-Line User Input and Add that to a Variable in Bash

We are using a construct using xargs and ctrl-d for breaking.  I’m not perfectly satisfied with it, but it certainly does the job of taking multi-line user input and stuffing that into a variable (formatting intact).

(The first and third assignments add quotes around the contents of the xargs input.  You may or may not require that.  In our case the variable is the body of a message sent by mail.  It must be quoted.)

##
#
printf '%s\n' "What would you like the body of the message to contain?  " 
printf "\\033[1mWhen finished hit ctrl-d on a new line to proceed.\\033[0m  \\n\\n" 
# this will load the user's input into a variable instead of a file 
reminderBody="\"" 
reminderBody+=$( xargs -0 ) 
reminderBody+="\"" 
## 

This basic construct could be used for a variety of needs.

(The \033[0m‘s are used to change font colors. Again, optional.)

Share

Leave a Reply

Your email address will not be published. Required fields are marked *