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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:54:04+00:00 2026-05-16T15:54:04+00:00

This is weird: using groovy strings to hold some command lines for execution, I

  • 0

This is weird: using groovy strings to hold some command lines for execution, I find that sometimes if there are quote characters inside the string, the execution simply silently fails.

WTF? (Updated, see below.)

Here’s my test program:

print " 1: " ; "grep nameserver /etc/resolv.conf".execute().text.eachLine {println it }          ; println ""
print " 2: " ; "grep 'nameserver' /etc/resolv.conf".execute().text.eachLine {println it }        ; println ""
print " 3: " ; """grep nameserver /etc/resolv.conf""".execute().text.eachLine {println it }      ; println ""
print " 4: " ; """grep "nameserver" /etc/resolv.conf""".execute().text.eachLine {println it }    ; println ""
print " 5: " ; """grep 'nameserver' /etc/resolv.conf""".execute().text.eachLine {println it }    ; println ""
print " 6: " ; "awk /nameserver/ /etc/resolv.conf".execute().text.eachLine{ println it }         ; println ""
print " 7: " ; "awk '/nameserver/' /etc/resolv.conf".execute().text.eachLine{ println it }       ; println ""
print " 8: " ; """awk "/nameserver/" /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
print " 9: " ; """awk '/nameserver/' /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
print "10: " ; """awk "/nameserver/{print \$2}" /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
return

Here’s the results:

$ groovy weird.groovy 
 1: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 2: 
 3: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 4: 
 5: 
 6: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 7: 
 8: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34
domain lnx.copansys.com
search lnx.copansys.com tst.copansys.com copansys.com

 9: 
10: 

Update

Here’s another example:

"bash -c echo Hello".execute().text.eachLine{ println it }
"bash -c echo 'Hello'".execute().text.eachLine{ println it }
"bash -c echo \'Hello\'".execute().text.eachLine{ println it }

Results:

groovy> "bash -c echo Hello".execute().text.eachLine{ println it } 
groovy> "bash -c echo 'Hello'".execute().text.eachLine{ println it } 
groovy> "bash -c echo \'Hello\'".execute().text.eachLine{ println it } 

'Hello!'

Observe that embedding a backslash-quoted ‘ doesn’t seem to help.

  • 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-16T15:54:04+00:00Added an answer on May 16, 2026 at 3:54 pm

    When you execute commands with String.execute(), they don’t get parsed by a command shell. The quotes are passed through to the actual commands executed; in this case, grep and awk.

    This is illustrated by replacing grep and awk with echo:

    print " 1: " ; "echo something".execute().text.eachLine {println it }          ; println ""
    print " 2: " ; "echo 'something'".execute().text.eachLine {println it }        ; println ""
    print " 3: " ; """echo something""".execute().text.eachLine {println it }      ; println ""
    print " 4: " ; """echo "something" """.execute().text.eachLine {println it }   ; println ""
    print " 5: " ; """echo 'something'""".execute().text.eachLine {println it }    ; println ""
    

    Which results in:

     1: something
    
     2: 'something'
    
     3: something
    
     4: "something"
    
     5: 'something'
    

    A simple workaround is to build the command line as a list of strings:

    ["awk", 'BEGIN { print "hello" }'].execute().text
    

    If you need finer control over the how the command is parsed, take a look at ProcessBuilder, the Java class String.execute() is built around.

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

Sidebar

Related Questions

I'm getting this weird error on some stuff I've been using for quite a
Am using an NSMutableSet that is working great, up until this weird SIGABRT error
There is this weird bug in my program that I'm trying to fix, but
This is weird... I'm using the MVC pattern in php (codeigniter) to display a
This is weird, I am currently using iTextSharp and I want to add a
This is a weird problem. I am using a dataform, and when I edit
Can anyone explain why using comments like this in css cause really weird effects
edit This question is solved! Having something weird. I'm using html { font-size: 100%
I'm using Python 3.1.2 (Mac OS X 10.6) and found this weird behavior (I'm
I just came across this weird problem that is happening in IE. In the

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.