Stepan Yakovenko
Stepan Yakovenko

Reputation: 9206

Multiline assignment in bash

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?

Upvotes: 8

Views: 15113

Answers (3)

Zombo
Zombo

Reputation: 1

The question implicitly requests single line output, as I will 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%

Output

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"

Output

$ ./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;

Upvotes: 5

ghoti
ghoti

Reputation: 46856

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

Upvotes: 9

matcheek
matcheek

Reputation: 5147

or alternatively

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

Upvotes: 7

Related Questions