Bash Heredocs#

To increase readability of your bash scripts, especially when running things as root, make good use of here documents (also known as heredocs).

Example#

The following example[1] executes a few commands as root. It also shows how heredocs can be nested by defining different limit strings. In this case there is an outer one (ENDOFSUDO) and an inner one (EOF).

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

sudo -i <<'ENDOFSUDO'

cat <<'EOF' > /etc/apt/sources.list.d/google-chrome.list
deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main
EOF

apt-get update
apt-get -y install google-chrome-stable
ENDOFSUDO