Horje
linux set multiline variable Code Example
linux set multiline variable
# in a bash script the following works:

#!/bin/sh

text="this is line one\nthis is line two\nthis is line three"
echo -e $text > filename

# alternatively:
text="this is line one
this is line two
this is line three"
echo "$text" > filename

# cat filename gives:
this is line one
this is line two
this is line three
linux set multiline variable
read -r -d '' my_variable << \
_______________________________________________________________________________

String1
String2
String3
...
StringN
_______________________________________________________________________________
linux set multiline variable
echo | tee /tmp/pipetest << EndOfMessage
This is line 1.
This is line 2.
Line 3.
EndOfMessage
linux set multiline variable
# You may use echo:

echo    "this is line one"   \
    "\n""this is line two"   \
    "\n""this is line three" \
    > filename
#It does not work if you put "\n" just before \ on the end of a line.


# Alternatively, you can use printf for better portability (I happened to have a lot of problems with echo):

printf '%s\n' \
    "this is line one"   \
    "this is line two"   \
    "this is line three" \
    > filename


# Yet another solution might be:

text=''
text="${text}this is line one\n"
text="${text}this is line two\n"
text="${text}this is line three\n"
printf "%b" "$text" > filename


#or

text=''
text+="this is line one\n"
text+="this is line two\n"
text+="this is line three\n"
printf "%b" "$text" > filename
linux set multiline variable
# If you're trying to get the string into a variable, another easy way is something like this:

USAGE=$(cat <<-END
    This is line one.
    This is line two.
    This is line three.
END
)

#If you indent your string with tabs (i.e., '\t'), the indentation will be stripped out. If you indent with spaces, the indentation will be left in.

#NOTE: It is significant that the last closing parenthesis is on another line. The END text must appear on a line by itself.




Shell

Related
bash echo can create file Code Example bash echo can create file Code Example
kdenlive dark mode for linux Code Example kdenlive dark mode for linux Code Example
Unexpected value 'RoundProgressModule in Code Example Unexpected value 'RoundProgressModule in Code Example
bash tokens in variable Code Example bash tokens in variable Code Example
Package "ngx-mask" has an incompatible peer dependency to "@angular/common" Code Example Package "ngx-mask" has an incompatible peer dependency to "@angular/common" Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12