Versions Compared

Key

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

...

  1. single quoting (e.g. 'some text') – this 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
  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 (more on globbing later...)
      • and some other metacharacters
    • double quoting also preserves any special characters in the text
      • e.g. newlines (\n) or 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 quote characters themselves ( '  "  ` ) are metacharacters that tell the shell to "start a quoting process" then "end a quoting process" when the matching quote is found. Since they are part of the processing, the enclosing quotes are not included in the output.

Let's look at examples of these.

Single and double quotes

The first rule of quoting is: always enclose a command argument in quotes if it contains spaces so that the command sees the Let's look at examples of these.

Single and double quotes

The first rule of quoting is: always enclose a command argument in quotes if it contains spaces so that the command sees the quoted text as one item.

To see more on how quoting affects text grouping, we'll use quotes to define some multi-word environment variables.

...

Code Block
languagebash
FOO="Hello world!"
echo "The value of variable 'FOO' is \"$FOO\""   # Escape the double quotes inside double quotes
echo "The value of variable 'FOO' is "' "'$FOO'"'   # Single-quoted text after double-quoted text

...

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 promptprompt.

Note that the quote characters themselves ( '  "  ` ) are metacharacters that tell the shell to "start a quoting process" then "end a quoting process" when the matching quote is found. Since they are part of the processing, the enclosing quotes are not included in the output.

Exercise 3-3

How would you output this text: The backslash character \ is used for escaping

...

titleAnswer...

A couple of possibilities: 

Code Block
languagebash
echo 'The backslash character \ is used for escaping'  # Single quotes inhibit metacharacter processing
echo "The backslash character \\ is used for escaping" # Escape the escape character

Multi-line text

If you want to output multi-line text, you can:

  • Start the text with a single or double quote
    • press Enter when you want to start a new line
    • keep entering text and Enter
    • until you're satisfied
    • enter the matching single or double quote then Enter
  • Use echo -e to "enable interpretation of backslash escapes"
    • Now we know what that means!
    • Note that backslash escapes include some that represent non-printable characters
      • e.g. newline/linefeed ( \n ), and tab ( \t )

Exercise 3-4

How would you output this text:

My
name is
Anna

...

titleAnswer...

A couple of possibilities: 

Code Block
languagebash
echo 'My
name is
Anna'

echo -e "My\nname\nis\nAnna"

Backtick evaluation quoting

backtick ( ` ) evaluation quoting is one of the underappreciated wonders of Unix.

The shell:

  • evaluates the expression/command inside the backtick marks ( ` )
  • the standard output of the expression replaces the text inside the backtick marks ( ` )

Examples, using the date function that just writes the current date and time to standard output, which appears on your Terminal.

...

languagebash

...

you output this text: The backslash character \ is used for escaping

Expand
titleAnswer...

A couple of possibilities: 

Code Block
languagebash
echo 'The backslash character \ is used for escaping'  # Single quotes inhibit metacharacter processing
echo "The backslash character \\ is used for escaping" # Escape the escape character


Multi-line text

If you want to output multi-line text, you can:

  • Start the text with a single or double quote
    • press Enter when you want to start a new line
    • keep entering text and Enter
    • until you're satisfied
    • enter the matching single or double quote then Enter
  • Use echo -e to "enable interpretation of backslash escapes"
    • Now we know what that means!
    • Note that backslash escapes include some that represent non-printable characters
      • e.g. newline/linefeed ( \n ), and tab ( \t )

Exercise 3-4

How would you output this text:

My
name is
Anna

Expand
titleAnswer...

A couple of possibilities: 

Code Block
languagebash
echo 'My
name is
Anna'

echo -e "My\nname\nis\nAnna"


Backtick evaluation quoting

backtick ( ` ) evaluation quoting is one of the underappreciated wonders of Unix.

The shell:

  • evaluates the expression/command inside the backtick marks ( ` )
  • the standard output of the expression replaces the text inside the backtick marks ( ` )

Examples, using the date function that just writes the current date and time to standard output, which appears on your Terminal.

Code Block
languagebash
date          # Calling the date command 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

# Assign a string including today's date to variable "today"
today="Today is: `date`"; echo $today  

The slightly odd syntax $(<some command>), is equivalent to `<some command>`, and can be easier to read when the command to be evaluated is complex.

Code Block
languagebash
echo "Today is: `date`"
echo "Today is $(date)"

Exercise 3-5

How would you output this text using a command to calculate the number of lines: The haiku.txt file has 11 lines

...

So far text we've been working with has been output to standard output, which I keep reminding you is mapped to your Terminal. But you can redirect text elsewhere.

...

You can also specify the tee -a option to append the input text to the file you specify.a option to append the input text to the file you specify.

Note that the > redirection metacharacter sends its output to a file, not to another program's standard input stream as with the | pipe metacharacter. (There are some cases where redirection involves something other than a file, but that's a topic for the Advanced Bash scripting class.)

The standard error stream

...

It is easy to not notice the difference between standard output and standard error when you're in an interactive Terminal session – because both outputs are sent to the Terminal. But they are separate streams, with different meanings.

When executing commands you will want to manipulate standard output and standard error appropriately – especially for 3rd party programs.

...

Code Block
languagebash
ls haiku.txt xxx.txt 1> stdoutout.txt 2>stderr2>err.txt
cat stdoutout.txt   # Displays "haiku.txt"
cat stderrerr.txt   # Displays "ls: cannot access 'xxx.txt': No such file or directory"

...

  1. Use a command-line program that lets you enter/edit text in a Terminal window (e.g. nano, vi/vim, emacs)
    1. nano is extremely simple and is a good choice as a first local text editor
      • warning: nano has a tendency to break long single lines into multiple lines
    2. vi and emacs are extremely powerful but also quite complex
  2. Use a text editor or IDE (Integrated Development Environment) program that runs on your local computer but has an SFTP (secure FTP) interface that lets you connect to a remote computer
    • E.g.,Komodo IDE (Windows & Mac) or Notepad++ (Windows). Both are no-cost.
    • Once you connect to the remote host, you can navigate its directory structure and edit files.
    • When you open a file, its contents are brought over the network into the text editor's edit window, then saved back when you save the file.
  3. Use software or protocols that allow you to "mount" remote server directories
    • Once mounted, the remote storage appears as a local volume/drive.
      • Then, you can use any text editor or IDE on your local computer to open/edit/save remote files.
    • Software programs that can mount remote data include ExpanDrive for Windows or Mac (costs $$, but has a free trial), TextWrangler for Mac.
    • Remote file system protocols include Samba (Windows, Mac) and NFS (Linux)

...

  • Ctrl-a - go to start of line
  • Ctrl-e - go to end of line
  • Ctrl-v - go forward one page
  • Arrow keys are also modified by Ctrl- (Windows) or Option- (Mac)
    • Ctrl-right-arrow (Windows) or Option-right-arrow (Mac) will skip by "word" forward
    • Ctrl-left-arrow (Windows) or Option-left-arrow (Mac) will skip by "word" backward

...