Search code examples
rlinuxbashsystem

How to run system command with quote


I have a bash command that I run like this:

esearch -db protein -query "AVA17449.1" | elink -target nuccore | efetch -format ft

but I want to do this in R like this (which doesn't work)

output <- system("esearch -db protein -query "AVA17449.1" | elink -target nuccore | efetch -format ft")

What would be the proper way of invoking this command within R?

P.S. esearch can be installed with the command below

cd ~
/bin/bash
perl -MNet::FTP -e \
    '$ftp = new Net::FTP("ftp.ncbi.nlm.nih.gov", Passive => 1);
    $ftp->login; $ftp->binary;
    $ftp->get("/entrez/entrezdirect/edirect.tar.gz");'
gunzip -c edirect.tar.gz | tar xf -
rm edirect.tar.gz
builtin exit
export PATH=$PATH:$HOME/edirect >& /dev/null || setenv PATH "${PATH}:$HOME/edirect"
./edirect/setup.sh

Solution

  • either use single quotes:

    cmd <- paste(c('esearch  -db protein -query "AVA17449.1"',
                   'elink -target nuccore',
                   'efetch -format ft'),
                  collapse=" | ")
    

    (the paste(...,collapse=...) stuff isn't necessary, but might make your code more readable ...)

    or protect the double-quote with backslashes: ... -query \"AVA17449.\" ...

    The single-quote is probably more readable, but backslashes can work in more complex situations where you need both kinds of quotes within a string ...