2

I'm trying to debug an larger osascript 'do shell script' program and have narrowed the problem down to a much shorter repro case that I don't understand at all. Here's the repro case:

osascript -e 'do shell script "echo 123; echo 45; echo 6"'

Gives me the output:

653

Can anybody explain what's going on? It's almost like 'do shell script' does not properly handle line endings, or tries to print everything on one line.

  • This very much looks like you have carriage returns in place of line feeds. – Charles Duffy Oct 29 '21 at 18:20
  • Same output from `osascript -e $'do shell script "printf \'%s\\n\' 123 45 6"'`, which is identical from what you'd see from `printf '%s\r' 123 45 6; echo` – Charles Duffy Oct 29 '21 at 18:23
  • This is very closely related to [CR vs LF in applescript](https://stackoverflow.com/questions/64386031/cr-vs-lf-in-applescript), though the `set AppleScript's text item delimiters to linefeed` therein isn't sufficient to resolve it. – Charles Duffy Oct 29 '21 at 18:24
  • Take a look at: `osascript -e $'set output to do shell script "printf \'%s\\n\' 123 45 6"\nset AppleScript\'s text item delimiters to return\nreturn text items of output'` – Charles Duffy Oct 29 '21 at 18:30

1 Answers1

3

Applescript replaces \n characters with \r characters when reading the output of a shell script. You can tell this is happening by running the output through od -c:

$ osascript -e 'do shell script "echo 123; echo 45; echo 6"' | od -c
0000000    1   2   3  \r   4   5  \r   6  \n                            
0000011

To turn this off, use the without altering line endings parameter.

 osascript -e 'do shell script "echo 123; echo 45; echo 6" without altering line endings'

See Technical Note TN2065 for more.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66