Using variables assigned with heredoc content in a Bash script via Sed

I’m trying to replace some configuration data in Ansible files with values that I have assigned with heredoc. The heredoc data contains values that are retrieved from Git.

The end goal is to retrieve Git config and update some Ansible var files with their values.

  • Ansible deployment git clone: “local variable 'remote_head' referenced before assignment”
  • Triggering Jenkins to run application tests when GitHub PR is created
  • Ansible SSH private key in source control?
  • Vagrant shell and ansible provisioning fail with bitbucket
  • How to fix Memory error in Ansible?
  • How to get the commit id of git while doing ansible deploy
  • I don’t know how to resolve the error

    sed: -e expression #1, char 6: unterminated `s' command

    The Bash script looks like the following

        # Attempt to get the most relevant git configuration (global, local then repo)
        GIT_USER=$(git config
        GIT_EMAIL=$(git config
        if [ -z "${GIT_EMAIL}" ] || [ -z "${GIT_USER}" ]; then
            echo "Please set up your Git credentials (User and Email)"
            exit 1
        # Update the ansible vars/all.yml with the Git configuration
        # This is the pattern we are looking for
        read -r -d '' SEARCH_CONFIG <<EOF
        install: '1'
        # This is the replacement
        read -r -d '' REPLACEMENT_CONFIG <<EOF
        install: '1'
            email: '${GIT_EMAIL}'
            name:. '${GIT_USER}'
        # Debugging
        #echo "${SEARCH_CONFIG}"
        #echo "${REPLACEMENT_CONFIG}"
        set -x
        # Do the replacement
        sed -i 's/'"${SEARCH_CONFIG}"'$/'"${REPLACEMENT_CONFIG}"/ "${CONFIG}"

    Perhaps I’m over-complicating the approach and there is a better alternative. I want to ensure that I only update the email and name keys when they exist under the git -> user keys.

    Edit –

    After looking at the answer from @chridd I am closer.

    The Bash output now looks like

    + sed -i 's/git:
        install: '\''1'\''
        install: \'\''1\'\''
            email: '\''foo@bar'\''
            name: '\''Foo Bar'\''/' test.yml
    sed: -e expression #1, char 6: unterminated `s' command

    I’m still doing something incorrectly. Also, I didn’t need to use the convoluted script to get the Git config (updated)

  • Understanding symfony2 deps
  • What is the URL to a heroku git repository?
  • How to merge Git pushed copy of branch
  • Replacing git submodules with nested repos workflow
  • Remove Remote Fork
  • Doing git-push without origin master
  • One Solution collect form web for “Using variables assigned with heredoc content in a Bash script via Sed”

    The problem is where it says $(echo "${SEARCH_CONFIG}"). echo outputs a newline after its output, which means that if $SEARCH_CONFIG is “foo” and $REPLACEMENT_CONFIG is “bar”, then the command sed is trying to execute looks like


    sed interprets this as three separate commands since it’s three separate lines, and complains since s/foo isn’t a complete command. In this case, the echo is unnecessary; just use ${SEARCH_CONFIG} directly without the echo:

        sed -i 's/'"${SEARCH_CONFIG}"/"${REPLACEMENT_CONFIG}"/ "${CONFIG}"

    If you specifically care about it being at the end of the line, then add a $ after the regular expression (make sure to quote it):

        sed -i 's/'"${SEARCH_CONFIG}"'$/'"${REPLACEMENT_CONFIG}"/ "${CONFIG}"
    Git Baby is a git and github fan, let's start git clone.