I'm writing a shell script for Mac OS/Linux. The script is run by a cron job. I'm trying to append stdout and stderr to a log file. My command(simplified) looks like this(line 5 in myscript.sh):

mycommand &>> log.txt

I get the following error:

myscript.sh: line 5: syntax error near unexpected token `>

However if I change the command to:

mycommand &> log.txt

I don't get any errors, but the file is overwritten (not appended).

Any idea why &>> doesn't work but &> works?

  • @KamilMaciorowski the command I showed is in line 5 in myscript.sh – Caner Apr 11 '18 at 12:16

cron is executed using sh and the redirection syntax you are attempting to use is a Bash extension (and a fairly recent one at that; older versions of Bash only supported &>).

The portable way to append standard output and standard error to a file is

mycommand >>log.txt 2>&1
  • 1
    excellent, this works and also explains the behaviour! – Caner Apr 11 '18 at 12:24

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.