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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:33:33+00:00 2026-06-10T07:33:33+00:00

These work as advertised: grep -ir ‘hello world’ . grep -ir hello\ world .

  • 0

These work as advertised:

grep -ir 'hello world' .
grep -ir hello\ world .

These don’t:

argumentString1="-ir 'hello world'"
argumentString2="-ir hello\\ world"
grep $argumentString1 .
grep $argumentString2 .

Despite 'hello world' being enclosed by quotes in the second example, grep interprets 'hello (and hello\) as one argument and world' (and world) as another, which means that, in this case, 'hello will be the search pattern and world' will be the search path.

Again, this only happens when the arguments are expanded from the argumentString variables. grep properly interprets 'hello world' (and hello\ world) as a single argument in the first example.

Can anyone explain why this is? Is there a proper way to expand a string variable that will preserve the syntax of each character such that it is correctly interpreted by shell commands?

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

    Why

    When the string is expanded, it is split into words, but it is not re-evaluated to find special characters such as quotes or dollar signs or … This is the way the shell has ‘always’ behaved, since the Bourne shell back in 1978 or thereabouts.

    Fix

    In bash, use an array to hold the arguments:

    argumentArray=(-ir 'hello world')
    grep "${argumentArray[@]}" .
    

    Or, if brave/foolhardy, use eval:

    argumentString="-ir 'hello world'"
    eval "grep $argumentString ."
    

    On the other hand, discretion is often the better part of valour, and working with eval is a place where discretion is better than bravery. If you are not completely in control of the string that is eval‘d (if there’s any user input in the command string that has not been rigorously validated), then you are opening yourself to potentially serious problems.

    Note that the sequence of expansions for Bash is described in Shell Expansions in the GNU Bash manual. Note in particular sections 3.5.3 Shell Parameter Expansion, 3.5.7 Word Splitting, and 3.5.9 Quote Removal.

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

Sidebar

Related Questions

<div id=table> <input type=checkbox class=db2applforce kon value=15010 title=to be forced> Neither of these work.
$('#menu .sub-right').css( 'paddingLeft', $('#menu .sub-left').width() ); nor $('#menu .sub-right').css({'padding-left': $('#menu .sub-left').width()}); ...these work. What
These two queries work: SELECT u.id, u.email, COUNT(*) as Current FROM users u, rounds
I'm looking for a tutorial explaining how to work with these 3 technologies, found
We're having a hard time figuring how these credentials objects work. In fact, they
These 2 ways of working both work, but I'm wondering if there's a difference
Intro I work in a facility where we have microscopes. These guys can be
I'm trying to make this fragment work: Version History --------------- These are the versions
Our company does work environment surveys, and these surveys are filled in online. All
My string comparison doesnt work.I tried compare,equals ,= and all of these methods doesnt

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.