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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:41:37+00:00 2026-05-31T07:41:37+00:00

I am trying to pass bash user input read using the read command to

  • 0

I am trying to pass bash user input read using the read command to an external application, as if what the user wrote were arguments in bash.

In the following example, I’ve used a function rather than an external application, but that should be irrelevant. This is the script I am using:

foo() { while [ "$#" -gt 0 ]; do echo "$1"; shift; done }
read -p "Write something: " bar
foo "${bar}"
foo ${bar}

What I write when I am asked to write something:

"a b" "c d" e\ f g\"h

What I expect to see as output:

a b
c d
e f
g"h

What I actually see: (the first line is first call to foo(), the rest of the lines are from the second call)

"a b" "c d" e f g"h
"a
b"
"c
d"
e
f
g"h
  • 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-05-31T07:41:38+00:00Added an answer on May 31, 2026 at 7:41 am

    If you run set -x before you start, bash will echo the commands as it’s trying to run them. In particular, you can see that bash is expanding $bar before it gets passed to foo():

    $ foo ${bar}
    + foo '"a' 'b"' '"c' 'd"' e f 'g"h'
    

    The answer is to use eval, however this isn’t quite as straightforward as it might seem:

    $ eval foo ${bar]
    + eval foo '"a' 'b"' '"c' 'd"' e f 'g"h'
    bash: unexpected EOF while looking for matching `"'
    bash: syntax error: unexpected end of file
    

    In this case, you need to escape both the quote and the backslash:

    $ read -p "Write something: " bar
    + read -p 'Write something: ' bar
    Write something: "a b" "c d" e\ f g\\\"h
    $ eval foo ${bar}
    + eval foo '"a b" "c d" e f g\"h'
    ++ foo 'a b' 'c d' e f 'g"h'
    ++ '[' 5 -gt 0 ']'
    ++ echo 'a b'
    a b
    ++ shift
    ++ '[' 4 -gt 0 ']'
    ++ echo 'c d'
    c d
    ++ shift
    ++ '[' 3 -gt 0 ']'
    ++ echo e
    e
    ++ shift
    ++ '[' 2 -gt 0 ']'
    ++ echo f
    f
    ++ shift
    ++ '[' 1 -gt 0 ']'
    ++ echo 'g"h'
    g"h
    ++ shift
    ++ '[' 0 -gt 0 ']'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a string variable to an application in a bash script:
I have a bash script where I'm trying to pass posix style arguments with
I'm trying to pass in a Base64 string into a C#.Net web application via
I am trying to pass a dataString to to an ajax call using JQuery.
I'm trying to run multiple instances of a node server using a bash script.
Iam trying to pass dynamic argument values i.e username from request using spring ioc.But
im trying to pass back from a user control a list of strings that
I'm trying to pass a command parameter with my command. I have commands in
I am trying to pass an array to a ruby script from a command
Inside a bash script function, I need to work with the command-line arguments of

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.