Versions Compared

Key

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

Table of Contents

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

...

As you can see, echo just takes all the text following the command name and writes it back out to standard output – the Linux stream that by default is mapped to your terminalTerminal. So the text appears on your terminalTerminal!

A couple of useful options:

  • echo -e says to enable interpretation of backslash escapes
    • so, for example, \n is interpreted as a linefeed, and \t as a tab character
  • echo -n says don't output the trailing newline (linefeed) character

...

Tip

Careful – do not put spaces around the equals sign when assigning environment variable values! The shell is very picky about this.

Also, variable names can only contain alphnumeric (A-Z, a-z, 0-9) and underscore ( _ ) characters, and must begin with a letter.

...

It's perfectly fine to evaluate an environment variable that has not been assigned a value - it will just have no valuebe "empty".

Code Block
languagebash
echo The value of variable XYZZY is: $XYZZY

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 all along with their values.

...

  • Use the backslash ( \ ) character that escapes the following character
    • escaping means treat the next character as a literal, even if it is a special metacharacter.
  • Use combinations of single and double quotes.

...

Expand
titleAnswer...

A couple of possibilities: 

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


...

  • 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 ).
  • Use a heredoc to define a block of text (more on this later)

...

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

...

How would you output this text , but 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.

Recall the three standard Unix streams: they each have a number, a name and redirection syntax:

...

Code Block
languagebash
echo hello > out.txt
cat out.txt              # Displays "hello"

echo world 1>> out.txt   # Appends "world" to out.txt
cat out.txt              # Displays "hello" "world" on 2 lines

echo goodbye world 1>out.txt  # Overwrites out.txt file
cat out.txt                   # Displays "goodbye world"

Notice that when using redirection, the output does not appear on the Terminal, it only goes to the specified file.

If you want output to go to both the Terminal and a file, you can use the tee command:

...

In addition to the methods of writing multi-linetextline text previously discussed, there's another one that can be useful for composing a large block of text for output to a file. This is done using the heredoc syntax to define a block of text between two user-supplied block delimiters, sending the text to a specified command.

...

  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, although it may be slower than local file editing
    • 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)

...