Versions Compared

Key

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

...

  • 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)

Exercise 3-4

How would you output this text:

...

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

Writing multiple text lines using a heredoc

In addition to the methods of writing multi-line 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.

The general form of a heredoc is:

Code Block
languagebash
COMMAND << DELIMITER
..text...
..text...
DELIMITER

For example, using the delimiter EOF and the cat command:

Code Block
languagebash
cat << EOF
This text will be output
And this USER environment variable will be evaluated: $USER
EOF

Here the block of text provided to cat is just displayed on the Terminal. To write it to a file just use the 1> or > redirection syntax after the block delimiter you name:

Code Block
languagebash
cat << EOF 1> out.txt
This text will be output
And this USER environment variable will be evaluated: $USER
EOF

The out.txt file will then contain this text:

Code Block
languagebash
This text will be output
And this USER environment variable will be evaluated: student01
Tip

The 2nd (ending) block delimiter you specify for a heredoc must appear at the start of a new line.

Editing text

...


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?

There are two three main approaches to editing Unix files:

  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)

...

You'll see the name of the file (if you supplied one) on the top line of the Terminal windowline of the Terminal window.

Navigation and operations in nano are similar to those we discussed in Command line editing.

You can just type in text, and navigate around using arrow keys (up/down/left/right). A couple of other navigation shortcuts:shortcuts:

  • Ctrl-a - go to start of line
  • Ctrl-e - go to end of line
  • 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-
    a - go to start of lineCtrl-e - go to end of line
    • left-arrow (Windows) or Option-left-arrow (Mac) will skip by "word" backward

Once you've positioned the cursor where you want it, just type in your text.

To remove text:

...

the cursor where you want it, just type in your text.

To remove text:

  • To delete text after the cursor, use:
    • Delete key on Windows
    • Function-Delete keys on Macintosh
  • To delete text before the cursor, use:
    • Backspace key on Windows
    • Delete key on Macintosh
  • Use Ctrl-k (kill) to delete everything on the line
    • This is different from Ctrl-k on the command line where it deletes everything after the cursor

Once you're satisfied with your edits:

...

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.

...