0

I'm looking at a pre-commit git hook script. Here's the part I'm struggling with:

#!/bin/sh
...
testcmd="go test -race ${godirs} "
failed=
${testcmd} &> /dev/null # problematic line
if [ $? -ne 0 ] ; then
    failed=1
    ${testcmd}
fi

godirs is a list of folders built earlier in the script. When I run this, I see the output of testcmd running slightly delayed (it prints over my next prompt). It seems that &> isn't redirecting 2 outputs. It's running the go test in the background and not even redirecting output.

I'm no good at shell scripting. I know I could rewrite it as > /dev/null 2> /dev/null but this is a learning opportunity. How can I fix this so I can still use &>?

Corey Ogburn
  • 461
  • 1
  • 6
  • 15
  • 1
    `&>` (as a redirection) is a [bashism](https://mywiki.wooledge.org/Bashism) - either change your shebang to `#!/bin/bash` or use the POSIX `> /dev/null 2>&1` – steeldriver Jul 09 '20 at 18:54
  • @steeldriver That sounds like a legitimate answer. You should post it. – Corey Ogburn Jul 09 '20 at 18:56
  • as an aside: [How can we run a command stored in a variable?](https://unix.stackexchange.com/q/444946/170373) – ilkkachu Jul 09 '20 at 19:06
  • Gilles's answer [here](https://unix.stackexchange.com/a/590707/170373) also has the explanation of how `&>` is interpreted in a POSIX shell, at the very end. – ilkkachu Jul 09 '20 at 19:08

0 Answers0