Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. single quoting (e.g. 'some text') – serves two purposes
    • it groups together all text inside the quotes into a single token
    • it tells the shell not to "look inside" the quotes to perform any evaluation
      • all metacharacters inside the single quotes are ignored
      • in particular, any environment variables in single-quoted text are not evaluated
    • single quoting preserves any whitespace present in the text (spaces or newlines)
  2. double quoting (e.g. "some text") – also serves two purposes
    • it groups together all text inside the quotes into a single token
    • it allows environment variable evaluation, but inhibits some metacharcters
      • e.g. asterisk ( * ) pathname globbing and some other metacharacters
    • double quoting also preserves any special characters present whitespace in the text
      • e.g. spaces, newlines (\n) or , and Tabs (\t)
  3. backtick quoting (e.g. `date`)
    • evaluates the expression inside the backtick marks ( ` )
    • the standard output of the expression replaces the text inside the backtick marks ( ` )
    • note that the syntax $(<expression>) is equivalent to `<expression>`

...

Tip

Always use single ( ' ) or double ( " ) quotes when you define an environment variable whose value contains spaces so that the shell sees the quoted text as one item.

Single vs double quotes examplesExamples:

Code Block
languagebash
echo "My Unix group is $MY_GROUP"   # The text "$MY_GROUP" is evaluated 
                                    #   and its value substituted  
echo 'My Unix group is $MY_GROUP'   # The text "$MY_GROUP" is left as-is

foo="My USER name is $USER"; echo $foo         # The text "$USER" is evaluated and
its value                                        #   and its value substituted
foo='My USER name is $USER'; echo $foo         # The text "$USER" is left as-is

FOO="Hello world!"
echo "The value of variable 'FOO' is \"$FOO\""  # Escape the double quotes inside double quotes
Tip
If you see the greater than ( > )

                                                #   inside double quotes

# Same output - the shell removes whitespace
echo Hello world!; echo Hello   world!

# Echo a multi-line text variable without quotes, and the
# shell removes whitespace (here the newline)
foo='aa
bb'; echo $foo

# But enclose the multi-line variable in double quotes and whitespace
# is preserved
echo "$foo"


Tip

If you see the greater than ( > ) character after pressing Enter, it can mean that your quotes are not paired, and the shell is waiting for more input to contain the missing quote of the pair (either single or double). Just use Ctrl-c to get back to the command prompt.

...

Code Block
languagebash
date          # Calling the date command just displays just displays 
              #   date/time information
echo date     # Here "date" is treated as a literal word, and 
              #   written to output
echo `date`   # The date command is evaluated and its output replaces
   the command  today=$( date );      #   replaces echothe $todaycommand

# environmentAssign variablea "today"string isincluding assigned today's date to variable "today"
today="Today is: `date`"; echo $today  # "today" is assigned a string including today's date

More at: Intro Unix: Writing text: Quoting in the shell

...

Code Block
languagebash
ls haiku.txt xxx.txt                # displays both output and error text 
                                   #   on the Terminal
ls haiku.txt xxx.txt 2>/dev/null    # displays only output text on the the 
                                   #   Terminal
ls haiku.txt xxx.txt 1>/dev/null    # displays only error text on the Terminal 
                    # And this syntax (2>&1) sends standard output to outerr.log and standard error to the #   sameTerminal
place
as# standardAnd out.this So data fromsyntax (2>&1) sends both standard output and standard error 
# to willthe same beplace writtenas tostandard outerrout.log 
ls haiku.txt xxx.txt 1>outerr.log 2>&1 

...