I want to create a script that can be called from the command line, but I can't figure out how to pass it a tab.
For example, here is my R script, saved as opt_tab.R
:
library(getopt)
opt <- getopt(matrix(c('string', 's', 1, "character", "Should be a tab"), byrow = TRUE, ncol = 5))
if(opt$string == "\t"){
cat("Success!\n")
} else{
cat("Failure...\n")
cat("Length of input: ", nchar(opt$string), "\n")
}
Then, from the command line (in my case using bash on WSL), I try different calls:
$ Rscript.exe opt_tab.R -s \t
Failure...
Length of input: 1
$ Rscript.exe opt_tab.R -s \\t
Failure...
Length of input: 2
$ Rscript.exe opt_tab.R --string=\t
Failure...
Length of input: 1
$ Rscript.exe opt_tab.R --string="\t"
Failure...
Length of input: 2
In all these cases, the \t
appears to be interpreted as a literal \
and t
. Is there a way to pass the argument correctly?
Note the problem appears similar if using commandArgs()
instead of {getopt}
.