Multi-line assignment in bash

In the .cmd files on the windows, I:

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 multi-line assignment in a shell?

+6
source share
3 answers

The question implicitly asks for a single output , as I show.

test.bat

 @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; @echo %JARS% 

Exit

 c:\home\Steven\Desktop>test.bat ./lib/apache-mime4j-0.6.jar;./lib/apache-mime4j-0.6.jar;./lib/bsh-1.3.0.jar;./li b/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; 

test.sh

 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;' echo "$JARS" 

Exit

 $ ./test.sh ./lib/apache-mime4j-0.6.jar;./lib/apache-mime4j-0.6.jar;./lib/bsh-1.3.0.jar;./li b/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; 
+4
source

There are so many ways to throw this cat away.

 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 gives you multi-line variable entry as per your question.

But if you plan to use these files in a shell script, you need to tell us how so that we can come up with the appropriate answers, instead of guessing us. For use in a shell script, files must be separated by something useful.

You asked, โ€œHow can I do such a multi-line assignment in a shellโ€, but the assignment in your example is actually a SINGLE line with ^ at the end of each input line, negating the next line of the new line (without avoiding it, as another answer suggested).

My solution in this answer is multi-line, but you will need to explain more about what you need to determine what will be useful.

For example, if you need to see a list of files that will be processed using the jar command, you might 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 
+5
source

or alternatively

 SOMEVAR=$( cat <<EOF value1 value2 value3 value4 value5 EOF ) 
+1
source

Source: https://habr.com/ru/post/919184/


All Articles