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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:06:09+00:00 2026-06-13T07:06:09+00:00

WHen I write a bash program I typically construct calls like follows: declare -a

  • 0

WHen I write a bash program I typically construct calls like follows:

declare -a mycmd=( command.ext "arg1 with space" arg2 thing etc )

"${mycmd[@]}" || echo "Failed: foo"

Where die foo is a bash function that prints Error foo and exits.

But if I want to be clear about the error reason, I want to print the failed command:

"${mycmd[@]}" || echo "Failed: foo: ${mycmd[*]}"

So the user can run the dead command and find out why. However, quoting is lost on this pass – the Failed message arguments that have spaces or escaped characters are not printed in a way that can be cut-n-pasted and run.

Does anyone have a suggestion for a compact way to fix this problem?


I think the problem is the way bash deals with argument parsing for commands, and the way (builtin) echo handles arguments. Another way of stating the problem is:

How can I print the quotes around arguments with spaces in the following bash example (which must be run as a script, not in immediate mode):

#!/bin/bash
mkdir qatest; cd qatest
declare -a myargs=(1 2 "3 4")
touch "${myargs[@]}"
ls
echo "${myargs[@]}"

actual result:

1  2  3 4
1 2 3 4

desired result

1  2  3 4
1 2 "3 4"

OR

1  2  3 4
"1" "2" "3 4"

In few additional bash code characters.

  • 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-13T07:06:10+00:00Added an answer on June 13, 2026 at 7:06 am

    Your problem is with echo. It is getting the correct number of parameters, with some parameters containing spaces, but it’s output loses the distinction of spaces between parameters and spaces within parameters.

    Instead, you can use printf(1) to output the parameters and always include quotes, making use of printf’s feature that applies the format string successively to parameters when there are more parameters than format specifiers in the format string:

    echo "Failed: foo:" $(printf "'%s' " "${mycmd[@]}")
    

    That will put single quotes around each argument, even if it is not needed:

    Failed: foo: 'command.ext' 'arg1 with space' 'arg2' 'thing' 'etc'
    

    I’ve used single quotes to ensure that other shell metacharacters are not mishandled. This will work for all characters except single quote itself – i.e. if you have a parameter containing a single quote, the output from the above command will not cut and paste correctly. This is likely the closest you will get without getting messy.

    Edit: Almost 5 years later and since I answered this question, bash 4.4 has been released. This has the "${var@Q}" expansion which quotes the variable such that it can be parsed back by bash.

    This simplifies this answer to:

    echo "Failed: foo: " "${mycmd[@]@Q}"
    

    This will correctly handle single quotes in an argument, which my earlier version did not.

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

Sidebar

Related Questions

I would like to write a bash script in Linux that executes a program
How to write command in bash ? ( for user test if program return
I have to write a bash script that makes lot of things. I'd like
I'm looking to write a Bash one-liner that calls a function once for each
I'm currently attempting to write my own program that mirrors the pmap command, specifically
i'm trying to write a php page that call for a server program like
I'm trying to write a BASH script to get my Java program to run(common
I have a bash program that will write to an output file. This file
I need to write a bash script to compile my java program. I know
I have one program that calls one tar.. something like popen(tar -zcvf) I want

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.