Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the `<<-EOSQL` code block in Bash when running SQL?

I need to execute a bash script containing SQL, so I am using a script to add custom configurations to a Postgres Docker container, according to the docs here:

https://github.com/docker-library/docs/tree/master/postgres#how-to-extend-this-image

But I don't know what EOSQL means. Here is an example of my script taken from the docs above:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
    CREATE EXTENSION $MY_EXTENSION;
EOSQL

So, what is EOSQL? I cannot seem to find much information about this command or keyword.

like image 840
modulitos Avatar asked Aug 06 '16 03:08

modulitos


1 Answers

EOSQL is a limit string for a Bash Here Document block. It signifies the start and end of a text block to the bash interpreter. The limit string can be any text that doesn't appear in your block.

Variable substitution will work as normal in a here document:

#!/usr/bin/env bash
cat <<-EOF
$MY_EXTENSION
EOF

Then running that with the variable set:

$ MY_EXTENSION=something ./test.sh
something

In Docker you will need ENV MY_EXTENSION=something in your Dockerfile or docker run -e MY_EXTENSION=something <image> on your command line for the environment to be setup.

like image 107
Matt Avatar answered Oct 25 '22 23:10

Matt