1

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?

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

1 Answer 1

4

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
  • 1
    excellent, this works and also explains the behaviour!
    – Caner
    Apr 11, 2018 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .