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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:13:38+00:00 2026-06-01T10:13:38+00:00

I’m constantly confused by the rules for quoting and evaluating when I’m writing bash

  • 0

I’m constantly confused by the rules for quoting and evaluating when I’m writing bash scripts. I know some of the basics, like the difference between ” and “” and “, but I still seem to get it wrong far too often, and be reduced to experimenting with trying all sorts of different ways to say the same thing.

Any individual problem I can usually work out by brute force, but I think my conceptual model of how it works must be hopelessly broken in some unknown way.

I have no problem with lisp’s quote,eval,read,print,syntax-quote system. In fact I wrote a little kata to help people understand what was going on: http://www.learningclojure.com/2010/11/syntax-quote-kata-for-confused.html

I guess that I’m looking for something similar for bash, (which seems much more complicated). A good model, or a set of exercises that will help me form such a model, that will make me able to look at a complicated shell script where variables are being transformed and evaluated and printed out and read, and work out what’s going to happen without having to try it.

Failing that, good methods of debugging the process, and watching what’s going on at each stage of the evaluation would be really helpful.

  • 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-01T10:13:39+00:00Added an answer on June 1, 2026 at 10:13 am

    Bruce Barnett’s UNIX Shell Quote Tutorial is awesome, and the Bash FAQ/pitfalls/word splitting articles have tons of useful tips. A short summary:

    Unquoted strings can contain most characters, but not all (like newlines) , and many of them (including space) will have to be escaped. Just don’t use them – If you fall for the temptation you might find that someone who modified the script forgot to include quotes once they became necessary.

    Single quoted strings can contain most characters, including NUL and newlines, but not single quotes, so they are also useful only for simple values.

    Backticks are for commands. They should only be used if your shell does not support $(). Example:

    current_dir=`pwd` # BAD! Don't do this!
    

    That command is bad, because when the right hand side of an assignment is not quoted the shell performs word splitting on it. It often leads to hard-to-reproduce bugs, because whitespace is difficult to check visually. To quote commands you have to use double quotes:

    current_dir="$(pwd)" # OK, but loses newlines at EOF
    

    Newlines at EOF are especially tricky. You can add a single character and strip it by using for example

    # Works for some commands, but not pwd
    current_dirx="$(pwd; echo x)"
    current_dir="${current_dirx%x}"
    printf %s "$current_dir"
    

    , but there’s an additional difficulty because some commands (like pwd) will add a newline at the end of their output anyway, so you might have to remove that as well:

    # Works for some commands, including pwd
    current_dirx="$(pwd; echo x)"
    current_dir="${current_dirx%$'\nx'}"
    printf %s "$current_dir"
    

    Double quotes can contain any character (Try echo -ne "\0" | wc -c), but note that variables can’t contain the NUL character.

    ANSI-C quotes can contain any characters except NUL (Try echo -ne $'\0' | wc -c), and provides handy escape codes to make it easier to work with special characters:

    printf %s $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    printf %q $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    touch -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    rm -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.