Creating an output file with multi line script using echo / linux

Zaza picture Zaza · Nov 12, 2016 · Viewed 25.9k times · Source

Trying to create a small script capable to write a part off the script in the output file without any changes, (as is)

source file text

echo "
yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0
echo"#${green}Installing packages${reset}#" &&
" >> output.txt

Desired output:

yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0
echo"#${green}Installing packages${reset}#" &&

What I'm getting is:

yellow=^[[33m
bel=^G
red=^[[31m
green=^[[32m
reset=^[(B^[[m
echo"#${green}Installing packages${reset}#" &&

Using CentOS 7 Minimal fresh install Looking for a solution to be applied to the full script/text, no the line per line changes, I suppose may be done using sed too ...

Answer

Nick picture Nick · Nov 12, 2016

You need to escape the backticks (`):

#!/bin/bash
echo "
yellow=\`tput setaf 3\`
bel=\`tput bel\`
red=\`tput setaf 1\`
green=\`tput setaf 2\`
reset=\`tput sgr0\`
" >> output.txt

As a bonus:

I prefer using this method for multiline:

#!/bin/bash
cat << 'EOF' >> output.txt
yellow=$(tput setaf 3)
bel=$(tput bel)
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
EOF