Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:38:56+00:00 2026-06-04T00:38:56+00:00

I am trying to run a command from a bash script. The command itself

  • 0

I am trying to run a command from a bash script. The command itself has single quotes in it, but I can’t get them to work as there are variables inside the single quotes that need to be expanded. Here is the bash script:

#!/bin/bash

if [ "$2" == "PracSci" ];
then
    echo "Running tabulate.science.r"
    R CMD BATCH --no-save --no-restore --slave '--args $1 $2 $3' tabulate.science.r /dev/tty
else
    echo "Running tabulate.r"
    R CMD BATCH --no-save --no-restore --slave '--args $1 $2 $3' tabulate.r /dev/tty
fi

When run as ./script.sh 2 Vocab May12 for example, the command itself is run without the variables values.

How can I expand the variables while still getting the command to run?

Just to be clear, the command I am trying to run is:
R CMD BATCH --no-save --no-restore --slave '--args $1 $2 $3' tabulate.r /dev/tty (including the single quotes in the command).

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:38:58+00:00Added an answer on June 4, 2026 at 12:38 am

    Short answer: use double-quotes instead of single-quotes (as @Pavel suggested). And when in doubt, use set -x to find out how the shell is parsing your commands.

    Long answer: quotes aren’t passed to the command; instead, they are parsed (and removed) by the shell, before the arguments are passed to the command. What they do is to change the way the shell parses what’s inside the quotes (generally by suppressing the special meanings of some characters), which means that you need to choose the appropriate quotes to allow the special characters you want parsed, and suppress the ones you want treated as just plain characters. For instance, consider the following command:

    R CMD BATCH --no-save --no-restore --slave '--args 2 Vocab May12' tabulate.r /dev/tty
    

    The shell will ignore the special meanings of all characters in the single-quoted section of the command. The only characters with any special meaning there are the spaces, which normally act as separators between arguments; in this case, the single-quotes make the shell treat them as part of a single argument. These other commands:

    R CMD BATCH --no-save --no-restore --slave "--args 2 Vocab May12" tabulate.r /dev/tty
    R CMD BATCH --no-save --no-restore --slave --args' '2' 'Vocab' 'May12 tabulate.r /dev/tty
    R CMD BATCH --no-save --no-restore --slave --args\ 2\ Vocab\ May12 tabulate.r /dev/tty
    

    …all do exactly the same thing, because they all (one way or another) get the shell to treat the spaces as part of an argument, rather than separators between arguments.

    Now, let’s look at the command that isn’t working for you:

    R CMD BATCH --no-save --no-restore --slave '--args $1 $2 $3' tabulate.r /dev/tty
    

    The problem is that while the single-quotes are suppressing the special meaning of the spaces (as you want), they’re also suppressing the special meaning of the $s (which you don’t want). So you need something more selective. One option would be to quote/escape the spaces but leave the $1 etc unquoted:

    R CMD BATCH --no-save --no-restore --slave --args' '$1' '$2' '$3 tabulate.r /dev/tty
    R CMD BATCH --no-save --no-restore --slave --args\ $1\ $2\ $3 tabulate.r /dev/tty
    

    (note that both of these commands do exactly the same thing.) These would mostly work, but have a bit of a potential for trouble: the shell will replace $1 etc with the arguments to the script, but then it does some additional parsing on them: looking for spaces to use as argument separators, wildcards to do filename matching on, etc. All things I’m pretty sure you don’t want — although since the arguments probably won’t contain any special characters this probably won’t be an issue. Probably.

    The best option I see is to simply use double-quotes:

    R CMD BATCH --no-save --no-restore --slave "--args $1 $2 $3" tabulate.r /dev/tty
    

    Double-quotes suppress the special meaning of spaces(as you want), allow $ to trigger variable expansion (also as you want), BUT prevent any further parsing once the variables have been expanded (also probably what you want). Spaces or other funny characters in arguments might still cause trouble within the R script, but at least this prevents them from causing trouble before the R script is even started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to invoke a native Windows command from Cygwin using run , but
I'm trying to run a bash script in linux (ubuntu but also fedora) but
I have been trying to run some java programs from a bash script in
Trying to run Jison unit tests, but the command fails. How do I fix
I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I am trying to run a Django management command from cron. I am using
I have a script that I'm trying to run from cron. When I run
I'm trying to create a FUNCTION in my Postgres database from a Bash script.
Can someone help me check my bash script? i'm trying to feed a directory
I'm trying to run some third party bash scripts from within my ruby program.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.