I have two scripts namely variables.sh and install.sh. When I execute bash install.sh, the .config file is not properly created. The export CC line doesn't adds BUILD64 variable entry into it and misses to add the " at the end of the line.

Snippet of variables.sh:

CC="${TARGET}-gcc ${BUILD64}"

Snippet of install.sh:

cat > "${INSTALL_DIR}/.config" << "EOF"
#!/usr/bin/env bash

echo "export INSTALL_DIR=\""${INSTALL_DIR}\""" >> "${INSTALL_DIR}/.config"
echo "export TARGET=\""${TARGET}\""" >> "${INSTALL_DIR}/.config"
echo "export BUILD64=\""${BUILD64}\""" >> "${INSTALL_DIR}/.config"
echo "export CC=\""${CC}\""" >> "${INSTALL_DIR}/.config"

source "${INSTALL_DIR}/.config"

Snippet of .config

#!/usr/bin/env bash
export INSTALL_DIR="/tmp/panda64"
export TARGET="x86_64-panda-linux-gnu"
export BUILD64="-m64"
export CC="x86_64-panda-linux-gnu-gcc

I tried using shellcheck program to see where I've been going wrong. But it also doesn't points out my mistake. I would appreciate if someone would point me into the right direction.


try instead

cat > "${INSTALL_DIR}/.config" << EOF
#!/usr/bin/env bash
export TARGET="${TARGET}" 
export BUILD64="${BUILD64}" 
export CC="${CC}"

source "${INSTALL_DIR}/.config"

you should still get variable expansion inside the heredoc, but you can get rid of messing quoting.

  • You will need to use EOF without quotes to get variable expansion within the here doc – steeldriver May 13 '16 at 13:28
  • @steeldriver I just tested now and you are right. I assumed bash here docs treated " the same as in echo etc. – the_velour_fog May 13 '16 at 13:33
  • No problem - test before post is my motto ;) – steeldriver May 13 '16 at 13:36
  • So the first EOF needs quotes in your answer, the second does not? Can you confirm, or better just edit your anwer? – Volker Siegel May 13 '16 at 14:53

try running your scripts through bash's explaination mode -x

bash -x install.sh 

This will show the return values of commands and the contents of variables which will highlight where you are going wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.