How to write multiple line string using Bash with variables?

LinuxBashBash4

Linux Problem Overview


How can I write multi-lines in a file called myconfig.conf using BASH?

#!/bin/bash
kernel="2.6.39";
distro="xyz";

echo <<< EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL >> /etc/myconfig.conf;
cat /etc/myconfig.conf;

Linux Solutions


Solution 1 - Linux

The syntax (<<<) and the command used (echo) is wrong.

Correct would be:

#!/bin/bash

kernel="2.6.39"
distro="xyz"
cat >/etc/myconfig.conf <<EOL
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

cat /etc/myconfig.conf

This construction is referred to as a Here Document and can be found in the Bash man pages under man --pager='less -p "\s*Here Documents"' bash.

Solution 2 - Linux

#!/bin/bash
kernel="2.6.39";
distro="xyz";

cat > /etc/myconfig.conf << EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL

this does what you want.

Solution 3 - Linux

If you do not want variables to be replaced, you need to surround EOL with single quotes.

cat >/tmp/myconfig.conf <<'EOL'
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 
EOL

Previous example:

$ cat /tmp/myconfig.conf 
line 1, ${kernel}
line 2, 
line 3, ${distro}
line 4 line
... 

Solution 4 - Linux

The heredoc solutions are certainly the most common way to do this. Other common solutions are:

echo 'line 1, '"${kernel}"'
line 2,
line 3, '"${distro}"'
line 4' > /etc/myconfig.conf

and

exec 3>&1 # Save current stdout
exec > /etc/myconfig.conf
echo line 1, ${kernel}
echo line 2, 
echo line 3, ${distro}
...
exec 1>&3  # Restore stdout

and

printf "%s\n" "line1, ${kernel}" "line2," "line3, $distro" ...

Solution 5 - Linux

I'm using Mac OS and to write multiple lines in a SH Script following code worked for me

#! /bin/bash
FILE_NAME="SomeRandomFile"

touch $FILE_NAME

echo """I wrote all
the  
stuff
here.
And to access a variable we can use
$FILE_NAME  

""" >> $FILE_NAME

cat $FILE_NAME

Please don't forget to assign chmod as required to the script file. I have used

chmod u+x myScriptFile.sh

Solution 6 - Linux

Below mechanism helps in redirecting multiple lines to file. Keep complete string under " so that we can redirect values of the variable.

#!/bin/bash
kernel="2.6.39"
echo "line 1, ${kernel}
line 2," > a.txt
echo 'line 2, ${kernel}
line 2,' > b.txt

Content of a.txt is

line 1, 2.6.39
line 2,

Content of b.txt is

line 2, ${kernel}
line 2,

Solution 7 - Linux

I usually put template in file and use this templating engine:

### <template-file> [ARG=VALUE..]
## Variables are replaced only within "{{" and "}}" notation.
## Example:
##         $0 path-to-tmpl REF=master pass=xx
##         # The template may look like so:
##         #    $pass = ["user", "{{ $pass }}"];
##         # Resulting in:
##         #    $pass = ["user", "xxx"];
##~
template() {
    tmpl=$1
    shift

    for i in $@; do
        declare $i;
    done

    eval "echo \"$(sed -e 's/"/\\"/g' -e 's/\$/\\$/g' -e 's/{{\s*\\\(\$\w*\)\s*}}/\1/g' $tmpl)\""
}

Solution 8 - Linux

another simpler way I think but definitely for small number of lines

touch myfile.txt
echo "line1">>myfile.txt
echo "line2">>myfile.txt
echo "line3">>myfile.txt
echo "line4">>myfile.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser285594View Question on Stackoverflow
Solution 1 - LinuxktfView Answer on Stackoverflow
Solution 2 - LinuxKentView Answer on Stackoverflow
Solution 3 - LinuxTk421View Answer on Stackoverflow
Solution 4 - LinuxWilliam PursellView Answer on Stackoverflow
Solution 5 - LinuxPratikView Answer on Stackoverflow
Solution 6 - LinuxrashokView Answer on Stackoverflow
Solution 7 - LinuxanimaacijaView Answer on Stackoverflow
Solution 8 - LinuxKat Lim RuizView Answer on Stackoverflow