Pass a tab as argument to getopt from command line

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}.

Found an answer!

The solution is:

$ Rscript.exe getopt.R --string="$(echo -e '\t')"

Note that the exact use of these quotes is important.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.