In .cmd files on windows I do:
SET JARS=^
./lib/apache-mime4j-0.6.jar;^
./lib/apache-mime4j-0.6.jar;^
./lib/bsh-1.3.0.jar;^
./lib/cglib-nodep-2.1_3.jar;^
./lib/commons-codec-1.6.jar;^
./lib/commons-collections-3.2.1.jar;^
./lib/commons-exec-1.1.jar;^
./lib/commons-io-2.0.1.jar;^
./lib/commons-io-2.3.jar;
How can I do such multiline assignment in shell?
Although Bash has various escape characters, we only need to concern ourselves with \n (new line character). For example, if we have a multiline string in a script, we can use the \n character to create a new line where necessary.
To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.
$2 is the second command-line argument passed to the shell script or function.
So many ways to skin this cat.
JARS='
./lib/apache-mime4j-0.6.jar;
./lib/apache-mime4j-0.6.jar;
./lib/bsh-1.3.0.jar;
./lib/cglib-nodep-2.1_3.jar;
./lib/commons-codec-1.6.jar;
./lib/commons-collections-3.2.1.jar;
./lib/commons-exec-1.1.jar;
./lib/commons-io-2.0.1.jar;
./lib/commons-io-2.3.jar;
'
This gets you multiline input in a variable, per your question.
But if you're planning to USE these files in a shell script, you need to tell us how, so that we can come up with appropriate answers, rather than making us guess. For use in a shell script, files need to be delimited by something useful.
You asked, "How can I do such multiline assignment in shell", but the assignment in your example is actually a SINGLE line, with the ^
at the end of each input line negating the following newline (not escaping it, as another answer suggested).
My solution in this answer is multiline, but you'll need to explain more about what you need this for in order to determine what will be useful.
For example, if you need to step through a list of files that will be processed with the jar
command, you might want to have something like:
#!/bin/sh
JARS='
./lib/apache-mime4j-0.6.jar
./lib/bsh-1.3.0.jar
...
'
set $JARS
for jarfile in "$@"; do
jar xf "$jarfile" ...
done
or alternatively
SOMEVAR=$( cat <<EOF
value1
value2
value3
value4
value5
EOF
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With