Search code examples
bashfunctionscopeheredoc

Bash : why variables are not read in the HEREDOC?


I have the file launcher

#! /bin/bash
. ./lib/utils

_name="microapplication-launcher"
_version="0.1"
_shortname="launcher"

DEFAULTS=$(cat <<'EOD'
# openning-prefs : terminal browser folder
launcher-openning-prefs=TRUE|FALSE|TRUE|
EOD
)
CONFIG="$( makeconfig )"

And the file ./lib/utils

__name="microapplication"
#export __name
__version="0.1"
#export __version

CONFIGDIR=~/.config/$__name
CONFIG=""
APPPATH="$HOME/Site/local"
export APPPATH

function makeconfig {
    # global config
    CONFIG="$CONFIGDIR/$__name.conf";


    [ ! -d $CONFIGDIR ] && mkdir $CONFIGDIR

    [ ! -f $CONFIG ] && cat <<"EOD" > $CONFIG
# $__name $__version
app-path=$APPPATH

EOD
}

When runing, it produce ~/.config/microapplication/microapplication.conf with

# $__name $__version
app-path=$APPPATH

The function is run in the launcher. The var __name ( 2 underscores) is well read in makeconfig for making the name of the file, but is no more read in the heredoc to produce the comment. Removing or not the underscores, exporting or not the vars seem having no effect.


Solution

  • Variables in a HEREDOC are not expanded if you quote the delimiter.

    Change

     cat <<"EOD"
    

    to

     cat <<EOD