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

  • Home
  • SEARCH
  • 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 7871213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:50:22+00:00 2026-06-03T01:50:22+00:00

I isolated a problem in my script to this small example. That’s what I

  • 0

I isolated a problem in my script to this small example. That’s what I get:

$ cmd="test \"foo bar baz\""
$ for i in $cmd; do echo $i; done
test
"foo
bar
baz"

And that’s what I expected:

$ cmd="test \"foo bar baz\""
$ for i in $cmd; do echo $i; done
test
"foo bar baz"

How can I change my code to get the expected result?


UPDATE Maybe my first example was not good enough. I looked at the answer of Rob Davis, but I couldn’t apply the solution to my script. I tried to simplify my script to describe my problem better. This is the script:

#!/bin/bash
function foo {
  echo $1
  echo $2
}

bar="b c"

baz="a \"$bar\""
foo $baz

This it the expected output compared to the output of the script:

expected  script
a         a 
"b c"     "b
  • 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-03T01:50:23+00:00Added an answer on June 3, 2026 at 1:50 am

    First, you’re asking the double-quotes around foo bar baz to do two things simultaneously, and they can’t. You want them to group the three words together, and you want them to appear as literals. So you’ll need to introduce another pair.

    Second, parsing happens when you set cmd, and cmd is set to a single string. You want to work with it as individual elements, so one solution is to use an array variable. sh has an array called @, but since you’re using bash you can just set your cmd variable to be an array.

    Also, to preserve spacing within an element, it’s a good idea to put double quotes around $i. You’d see why if you put more than one space between foo and bar.

    $ cmd=(test "\"foo bar baz\"")
    $ for i in "${cmd[@]}"; do echo "$i"; done
    

    See this question for more details on the special "$@" or "${cmd[@]}" parsing feature of sh and bash, respectively.

    Update

    Applying this idea to the update in your question, try setting baz and calling foo like this:

    $ baz=(a "\"$bar\"")
    $ foo "${baz[@]}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem that I discovered while I was implementing a small
This problem is happening is both IE8 and IE7. I have a small swf
I have fully isolated this problem to a very simple play app I think
I have an isolated python script that simply captures data from Twitter's streaming API
EDIT I isolated a real minimal example which does not work (it is a
I am attempting to a get a isolated build environment setup in my dev
I am not sure that I understand Isolated storage. I read the article http://msdn.microsoft.com/en-us/library/3ak841sy%28VS.80%29.aspx
When I try to create a Visual Studio Shell Isolated project, I get an
My problem is The following: I have a windows application that stores a file
I have an isolated problem with cached data in a web app, and understand

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.