Versions Compared

Key

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

We've looked at some ways of viewing text, so now we'll address how to write it.

Table of Contents

echo - the bash print function

Other programming languages usually supply a print statement or function that can direct text to a file or to standard output.

...

Code Block
languagebash
echo hello     # displays "hello" on the next terminal line, then the prompt on a new line
echo -n hello  # displays "hello" on the next terminal line followed by the prompt (no newline)

Exercise 3-1

What is the difference in character count when you echo hello with and without the -n option?

...

The reason for this difference is that by default, echo actually reads then outputs all the characters on the line, including the trailing linefeed. To understand this better, and to understand what is meant by "interpretation of backslash escapes", we need to first look at how the shell evaluates other metacharacters, and how that is affected by quoting in the shell.

Environment variables

Environment variables are just like variables in a programming language (in fact bash is a complete programming language): they are "names" that hold a value assigned to them. As with all programming language variables, they have two operations:

...

There are a number of pre-defined environment variables in the shell, such as USER (your account name) and PATH (more on PATH later). The env command will list them along with their values.

Exercise 3-2

Output a string that includes your account name and your Unix group name using environment variables.

...

Expand
titleAnswer...


Code Block
languagebash
env | grep -i group  # is there an environment variable with "group" in its name?

Examining the env output we find that the variable MY_GROUP contains our Unix group.

Code Block
languagebash
echo The Unix group for $USER is $MY_GROUP


Quoting in the shell

When the shell processes a command line, it first parses the text into tokens ("words"), which are groups of characters separated by whitespace (one or more space characters). Quoting affects how this parsing happens, including how metacharacters are treated and how text is grouped.

...

  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
  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 ( ` )

...

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.

...

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

Exercise 3-3

How would 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:

...

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.

...

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

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

...

Expand
titleAnswer...


Code Block
languagebash
echo "The haiku.txt file has `cat haiku.txt | wc -l` lines"

Notice that the backticked expression can be complex!

Redirection

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.

The standard error stream

So what's this standard error stream? Recall our discussion of Command input errors? Well, error information is written to standard error, not to standard output!

...

One final redirection trick. There is a special Linux file called /dev/null that serves as a "global trash can". That is – it just throws away anything you write to it. So you can direct standard output and/or standard error to /dev/null to ignore it completely.

Exercise 3-6

Show the difference between  standard output and standard error by redirecting standard error to /dev/null.

...

Expand
titleAnswer...

This will only display "ls: cannot access 'xxx.txt': No such file or directory"

Code Block
languagebash
ls haiku.txt xxx.txt 1> /dev/null


Editing text

We've now covered viewing existing file text and writing new text to a file. But what if you want to edit/change text in an existing file?

...

Knowing the basics of at least one Linux command-line text editor is useful for creating/editing small files, and we'll explore nano in this class. For editing larger files, you may find options #2 or #3 more useful.

nano

nano is a very simple editor available on most Linux systems. If you are able to ssh into a remote system, you can use nano there.

...

These and other important nano operations are displayed in a menu at the bottom of the Terminal window. Note that the ^ character means Ctrl- in this menu.

Edit your login script

Whenever you login to a remote server, a login script located in your Home directory is executed. This file, usually your ~/.profile and/or ~/.bashrc file, has expressions that customize your shell environment. These customizations are temporary – they are in effect only during your login sessions, which is why they have to be re-established every time you login.

On our system, ~/.profile file is your login script. Use more ~/.profile to look at it.

Exercise 3-7

Use nano to remove the pound sign ( # ) comment character from one of the two lines starting with #export LS_COLORS

...