You can concatentate variables and store multiple strings in the same one like so:
foo=abc
echo $foo # prints 'abc'
bar=123
foo="${foo}${bar}"
echo $foo # prints 'abc123'
You can use the other variables, or the same variable, when assigning to a variable, e.g. a="${a}123${b}"
. See this question for more info.
You don't have to quote the strings you're assigning to, or do the ${var}
syntax, but learning when to quote and not to quote is a surprisingly nuanced art, so it's often better to be safe than sorry, and the "${var}"
syntax in double quotes is usually the safest approach (see any of these links for more than you ever wanted to know: 1 2 3).
Anyway, you should read into a temporary variable (read, by default, reads into $REPLY
) and concatentate that onto your main variable, like so:
allinput=
read # captures user input into $REPLY
allinput="${REPLY}"
read
allinput="${allinput}${REPLY}"
Beware that the read
command behaves very differently depending on supplied switches and the value of the IFS
global variable, especially in the face of unusual input with special characters. A common "just do what I mean" choice is to empty out IFS via IFS=
and use read -r
to capture input. See the read
builtin documentation for more info.