Bug 16360 - readRenviron(".Renviron"): the first escaped quote is mishandled, the rest are ok
Summary: readRenviron(".Renviron"): the first escaped quote is mishandled, the rest ar...
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Startup (show other bugs)
Version: R 3.2.0
Hardware: Other Other
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-05-01 20:39 UTC by Bill Dunlap
Modified: 2015-05-01 20:39 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bill Dunlap 2015-05-01 20:39:43 UTC
If you use backslashes to escape single or double quotes in
a .Renviron file, then readRenviron will omit the first backslashed
quote but respect the others.

Here is a function to compare how readRenviron and bash (assumed to
be on your PATH) treat lines in a .Renviron file.

testRenviron <- function(envVarDef, tryBash = FALSE, envVarName = "wwd_5592")
{
    tf<-tempfile()
    Sys.unsetenv(envVarName)
    on.exit({Sys.unsetenv(envVarName); unlink(tf)})
    cat(file=tf, sep="", envVarName, "=", envVarDef, "\n")
    readRenviron(tf)
    value <- Sys.getenv(envVarName, unset="<not set>")
    Sys.unsetenv(envVarName)
    if (tryBash) {
        attr(value, "bashValue") <- system(paste0("bash -c ' . ", normalizePath(tf, winslash="/"), "; echo $", envVarName, "; '"), intern=TRUE)
    }
    value
}

They give the same results in most cases where the definition is legal in bash
  > testRenviron("${R_HOME-R_HOME is not defined}", tryBash=TRUE)
  [1] "/opt/sw/R/R-3.2.0/lib64/R"
  attr(,"bashValue")
  [1] "/opt/sw/R/R-3.2.0/lib64/R"
  > testRenviron("${T_HOME-T_HOME is not defined}", tryBash=TRUE)
  [1] "T_HOME is not defined"
  attr(,"bashValue")
  [1] "T_HOME is not defined"
  > testRenviron("\"T_HOME is not defined\"", tryBash=TRUE)
  [1] "T_HOME is not defined"
  attr(,"bashValue")
  [1] "T_HOME is not defined"
and R is a little more lenient about quotes than bash is
  > testRenviron("T_HOME is not defined", tryBash=TRUE)
  /tmp/RtmpY3wqim/file423b5464e774: line 1: is: command not found
  [1] "T_HOME is not defined"
  attr(,"bashValue")
  [1] ""

However, if you try to escape some quotes with backslashes R drops
the first backslashed quote (single or double) where bash treats them
all the same.
  > testRenviron("${LAWYERS-O\\'Malley, O\\'Finnegan, and O\\'Clock}", tryBash=TRUE)
  [1] "OMalley, O'Finnegan, and O'Clock"
  attr(,"bashValue")
  [1] "O'Malley, O'Finnegan, and O'Clock"
  > testRenviron("${STATEMENT-She said \\\"Hello\\\"}", tryBash=TRUE)
  [1] "She said Hello\""
  attr(,"bashValue")
  [1] "She said \"Hello\""
If both kinds of quotes are escaped odder things happen
  > testRenviron("${STATEMENT-She\\'d said \\\"Hello\\\"}", tryBash=TRUE)
  [1] "Shed said \\\"Hello\\\""
  attr(,"bashValue")
  [1] "She'd said \"Hello\""