1

I'm trying to source a multiline associative array like this

declare -p -A MOCK_RETURNS=(
  ['/usr/local/bin/docker']=""
  ['/usr/local/bin/docker history']="IMAGE               CREATED AT                  CREATED BY                                      SIZE                COMMENT
cf0f3ca922e0        -10-18T20:48:51+02:00   /bin/sh -c #(nop)  CMD ['/bin/bash']            0
<missing>           2019-10-18T20:48:51+02:00   /bin/sh -c mkdir -p /run/systemd && echo 'do…   7"
  ) > mockResponse

---- other file
source mockResponse
echo ${MOCK_RETURNS['/usr/local/bin/docker history']}

keeping the linebreaks in the /usr/local/bin/docker history-value in bash. Sadly it seems to be sourced as one line.

I already tried echoing it like

echo "declare -A MOCK_RETURNS=(
  ['/usr/local/bin/docker']=\"\"
  ['/usr/local/bin/docker history']=\"IMAGE               CREATED AT                  CREATED BY                                      SIZE                COMMENT
cf0f3ca922e0        $nextYear-10-18T20:48:51+02:00   /bin/sh -c #(nop)  CMD ['/bin/bash']            0
<missing>           2019-10-18T20:48:51+02:00   /bin/sh -c mkdir -p /run/systemd && echo 'do…   7\"
  )" > mockReturns

---- other file
source mockResponse
echo ${MOCK_RETURNS['/usr/local/bin/docker history']}

Which keeps shows the line breaks in the mockReturns file, but when I source it, they're gone again.

El Gohr
  • 177
  • 1
  • 12
  • The `declare` is only one command, no many how many lines you split it over. Why do you expect, or *want* for that matter, the line breaks to be retained in the output of `declare -p`? – Charles Duffy Oct 30 '19 at 14:44
  • The line breaks going away in the output is because you aren't quoting the argument to `echo`; it has nothing to do with associative arrays or the `source` command or whatnot. (If you still show the newlines when you run `echo ${MOCK_RETURNS['/usr/local/bin/docker history']}` manually, you must be doing something you aren't showing us, like modifying `IFS`). – Charles Duffy Oct 30 '19 at 14:45
  • ...in addition to the linked duplicate, see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy Oct 30 '19 at 14:47
  • See https://ideone.com/iGfB95 showing the same declaration with both `echo` commands (one with correct quoting, the other without). – Charles Duffy Oct 30 '19 at 14:50

1 Answers1

2

Try to echo the variable enclosed with ":

echo "${MOCK_RETURNS['/usr/local/bin/docker history']}"

$ declare -p -A MOCK_RETURNS=(
  ['/usr/local/bin/docker']=""
  ['/usr/local/bin/docker history']="IMAGE               CREATED AT                  CREATED BY                                      SIZE                COMMENT
cf0f3ca922e0        -10-18T20:48:51+02:00   /bin/sh -c #(nop)  CMD ['/bin/bash']            0
<missing>           2019-10-18T20:48:51+02:00   /bin/sh -c mkdir -p /run/systemd && echo 'do…   7"
) > mockResponse

$ cat test
#! /bin/bash
source mockResponse
echo "${MOCK_RETURNS['/usr/local/bin/docker history']}"
exit 0

$ ./test 
IMAGE               CREATED AT                  CREATED BY                                      SIZE                COMMENT
cf0f3ca922e0        -10-18T20:48:51+02:00   /bin/sh -c #(nop)  CMD ['/bin/bash']            0
<missing>           2019-10-18T20:48:51+02:00   /bin/sh -c mkdir -p /run/systemd && echo 'do   7

EDIT:
For declare, an -p option is useless if it's sourced as far as I know.

Bayou
  • 3,293
  • 1
  • 9
  • 22
  • @CharlesDuffy I figured it was an attempt, not part of the question. the `-p` option is useless if it's in a sourced file, since executing the script will only clutter the terminal. – Bayou Oct 30 '19 at 14:51
  • Yes, but it's not *in* a sourced file as the question is asked; it's how they're *creating* the sourced file. – Charles Duffy Oct 30 '19 at 14:51
  • Entirely correct. Changed it accordingly. – Bayou Oct 30 '19 at 14:53
  • Actually -p makes sense in my usecase. But it seems to be a diffrent problem, involving bats – El Gohr Oct 31 '19 at 09:46