This one stumped me. I have a simple shell script executing that works fine on my Linux (AWS aka CentOS) machine but crashed on my Mac OS X machine. It turned out escapes (\
) in string commands needed an extra escape character (\\
).
Could someone enlighten me as to what I am missing here -- ie, what is it about running the R scripts on Macs that require this?
The behavior was *not* observed when calling, say, python3 -c ..
On both machines, I am using bash
, specifically /bin/bash
NOTE: The Mac is a slightly later version of R: 3.5.1 vs 3.4.1, but I would be strongly surprised if that was the culprit. Anyone available to confirm?
Simple Example:
R --vanilla -e 'cat(" Hello \n World \n ")'
The above runs fine on a CentOS machine, but requires an additional escape character (\\n
instead of \n
) to execute correctly. (example at bottom)
For reference/comparison, the following python command works identically on each of the Mac OS X, CentOS machines I tested.
python3 -c 'print("Hello \n World")'
For details, here is the output comparing the two commands on each of the two machines
1. R --vanilla -e 'cat(" Hello \n World \n ")'
2. R --vanilla -e 'cat(" Hello \\n World \\n ")'
1.
R --vanilla -e 'cat(" Hello \n World \n ")'
## CENTOS:
> cat(" Hello \n World \n ")
Hello
World
## MAC OS X:
> cat(" Hello
+
+ Error: unexpected end of input
Execution halted
2.
R --vanilla -e 'cat(" Hello \\n World \\n ")'
## CENTOS:
> cat(" Hello \\n World \\n ")
Hello \n World \n >
## MAC OS X:
> cat(" Hello \n World \n ")
Hello
World
For comparison's sake, I'm not seeing the same behavior when running a simple python script.
## Each of these produce identical
## results in Mac OSX as CentOS
python3 -c 'print("Hello \n World")'
python3 -c 'print("Hello \\n World")'
Machine & Session Info:
- Linux Box
> cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
> R --vanilla -e 'sessionInfo()'
R version 3.4.1 (2017-06-30)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Amazon Linux AMI 2018.03
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1
- Mac OS Box
Mojave 10.14.3
> R --vanilla -e 'sessionInfo()'
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.14.3
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1
- Another Mac OSX machine, running 3.4.3, same error
> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
fish
, justbash
in both cases. I will edit the question – DelightfulR
wrapper shell script, it's pretty ugly in there (as in, rampant BashFAQ #50 violations) -- I'd need to dig in more to say anything conclusive, but it certainly smells like they could have introduced some bugs. – AlbanR
wrapper script executesexec "${R_binary}" ${args} "${@}"
. That's... well, follow the BashFAQ #50 link in the above comment for a description of why unquoted$args
is a practice nobody should ever, ever use. – AlbanR_HOME=/usr/lib/R /usr/lib/R/bin/exec/R -e 'cat(" Hello \n World \n ")'
behave consistently, after you fix up the paths to be correct for each platform? – Albansed
is in your PATH -- the host's BSDsed
, or GNUsed
installed with Nix/MacPorts/Homebrew/etc? (Though if that were related to the problem, bypassing the wrapper as suggested above should also avoid it). – Albansed
to munge command lines in R's wrapper script is an abomination, but that's neither here nor there; it exists, in the current implementation). – Albanbash --version
– Holmanbash --version
is the wrong way to check anyhow; it tells you which version is first in the PATH, not which version is currently running; thus, on MacOS, it shows any newer interpreter installed with Nix/MacPorts/Homebrew, even if Apple's/bin/bash
is currently in use. – AlbanR
script. 5. Running ShellCheck on it (and other scripts such asbuild
,INSTALL
, andcheck
) reveals this and other potential problems – Holmansed
to process commandline arguments. As mentionedCharles Duffy
the macOS (BSD) version ofsed
may have something to do with this. BSDsed
seems to not accept control sequences\n
and\t
(see this). This could explain the needed double escapes. However I may be completely wrong. – Cracknelsed
even on MacOS. Answering your other question: To check the running version of bash,declare -p BASH_VERSION
will do. – Albansed
) by executingR/bin/exec/R
directly, that should be able to rule out that theory (or increase its likelihood, by isolating the wrapper or something it calls as the source of the variance). – Alban