Multiline text in bash


automation bash

Multiline bash code pattern:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash -e

CUSTOM_VALUE=$( date )
ANOTHER_VALUE="String value:"
YET_ANOTHER_VALUE="More information"

set +e # read fail for some reason, disable error handling

# multiline variable
read -r -d '' MULTILINE_BASH_VARIABLE << EOL
The MULTILINE_BASH_VARIABLE contain this line
and all the rest until the

  'End Of Line' tag, which can be anything
  as long it is not in the multiline itself

Bash variables can be included 
Custom value: '$CUSTOM_VALUE'
${ANOTHER_VALUE}added:${YET_ANOTHER_VALUE}
EOL

set -e

# multiline file
cat > ./README << ANYTHING
The README file at call location will contain this line
  and all the rest until 
  
  'Anything' tag
  
Bash variables can be included 
Custom value: '${CUSTOM_VALUE}'
${ANOTHER_VALUE}added:${YET_ANOTHER_VALUE}
ANYTHING

printf "\n\n  OUTPUT: bash variable:\n\n"
echo "${MULTILINE_BASH_VARIABLE}"

printf "\n\n  OUTPUT: ./README file:\n\n"
cat "./README"
printf "\n\n"