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 8009555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:29:02+00:00 2026-06-04T18:29:02+00:00

I am making a Bash script that will print and pass complex arguments to

  • 0

I am making a Bash script that will print and pass complex arguments to another external program.

./script -m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'

How do I print the raw arguments as such?

-m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'

Using $@ and $*, removes the single quotes around uptime ; uname -a which could cause undesired results. My script does not need to parse each argument. I just need to print / log the argument string and pass them to another program exactly how they are given.

I know I can escape the quotes with something like "'uptime ; uname -a'", but I cannot guarantee the user will do that.

  • 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-04T18:29:03+00:00Added an answer on June 4, 2026 at 6:29 pm

    The quotes are removed before the arguments are passed to your script, so it’s too late to preserve them. But you can preserve their effect when passing the arguments to the inner command, and reconstruct an equivalent quoted/escaped version of the arguments for printing.

    For passing the arguments to the inner command "$@"—with the double quotes, $@ preserves the original word breaks, meaning that the inner command receives exactly the same argument list that your script did.

    For printing, you can use the %q format in Bash’s printf command to reconstruct the quoting. Note that this won’t always reconstruct the original quoting, but it will construct an equivalent quoted/escaped string. For example, if you passed the argument 'uptime ; uname -a' it might print uptime\ \;\ uname\ -a or "uptime ; uname -a" or any other equivalent (see William Pursell’s answer for similar examples).

    Here’s an example of using these:

    printf "Running command:"
    printf " %q" innercmd "$@" # note the space before %q -- this inserts spaces between arguments
    printf "\n"
    innercmd "$@"
    

    If you have Bash version 4.4 or later, you can use the @Q modifier on parameter expansions to add quoting. This tends to prefer using single quotes (as opposed to printf %q‘s preference for escapes). You can combine this with $* to get a reasonable result:

    echo "Running command: innercmd ${*@Q}"
    innercmd "$@"
    

    Note that $* mashes all arguments together into a single string with whitespace between them, which is normally not useful, but in this case each argument is individually quoted so the result is actually what you (probably) want. (Well, unless you changed IFS, in which case the "whitespace" between arguments will be the first character of $IFS, which may not be what you want.)

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

Sidebar

Related Questions

I'm trying to make a bash script that will talk to a java program
I'm trying to write a bash script that will recursively search a directory, find
I'm making a script in bash that requires files to be copied. I tried
I am making a bash script where I want to find files that are
I'm making a bash script that crawls through a directory and outputs all files
I am attempting to write a bash script and I am having difficulty making
The following is a fragment of a bash script that I'm running under cygwin
I have uncovered another problem in the effort that we are making to port
I have bash script that I use regularly in my job to automate a
I am making a shell script that takes a single number (length is unimportant)

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.