Versions Compared

Key

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

...

Code Block
languagebash
more jabberwocky.txt
cat jabberwocky.txt | more

Let's dissect the difference in detailThe main differences between these two commands:

  • In

...

  • more jaberwocky.txt, it is the more command that reads

...

  • the

...

  • then writes some output to standard output, which is displayed on your Terminal
  • it pauses at page boundaries (--More--) waiting for input on standard input
  • when it receives a space on on standard input it reads more input from jabberwocky.txt
  • then writes the output to standard output, which is displayed on your Terminal

...

  • file and performs its operations
    • displays some data on standard output, waits for a space on standard input, repeat until no more file data
    • since more is reading the file, it can display progress information about how much data has been read
  • In cat jabberwocky.txt | more, the cat program reads the file data and writes it to its standard output.
    • the pipe operator ( | ) then connects the standard output from cat to standard input of the more command
    • the more command then reads its input from standard input, instead of from a file

      ...

          • and causes the cat program to block – stop writing data to its standard output until

      ...

      • this "write until block" / "read when input wanted & available" behavior makes streams a very efficient means of inter-process communication.
          • requested
        • more displays some data on standard output, waits for a space on standard input, then requests more input
          • repeat until no more data from cat.
        • since the data coming in to more is from an anonymous pipe, more cannot display progress information

      More at: Intro Unix: Viewing text in files: Standard streams and piping

      echo, head, tail, cat -n, wc

      ...

      • With no options, head shows the first 10 lines of its input and tail shows the last 10 lines.
        • You can use the -n option to specify how many lines to view, or just put the number you want after a dash (e.g. head -5 for 5 lines or head -1 for 1 line).
      • To start viewing lines at a particular line number, use tail and put a plus sign (+) in front of the number (with or without the -n option).
      • The cat -n option adds line numbers to the text it displays, which can help orient you when dealing with large files

      Use the wc (word count) command to count text lines (wc -l) or characters (wc -c).

      echo is the bash command to output text.

      • 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

      ...

      Code Block
      languagebash
      head -n 5 haiku.txt                      # display the 1st 5 lines of "haiku.txt"  
      cat -n haiku.txt                         # display "haiku.txt" contents with line numbers        
      cat -n haiku.txt | tail -n 7             # display the last 7 lines of "haiku.txt"
      cat -n haiku.txt | tail -n +7            # display text in "haiku.txt" starting at line 7
      cat -n haiku.txt | tail -n +5 | head -3  # display the middle stanza of "haiku.txt"
      
      wc -l haiku.txt                          # count lines in "haiku.txt" file
      cat haiku.txt | wc -l                    # use wc -l to count lines of piped-in text
      
      echo 'Hello world!' | wc -c              # count characters output by echo, including the trailing newline
      echo -n 'Hello world' ! wc -c            # count characters output by echo, without the trailing newline
      
      echo -e "aa\nbb\ncc"                     # output 3 lines of text using \n to represent newlines

      More at:

      Other shell concepts

      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:

      ...

      In bash, you define (set/assign)an environment variable like this:

      Code Block
      languagebash
      varname='hello world!'  # # Assign the environment variable named "varname" the value "hello world!"
      varname='hello world!' 


      Tip
      • Do not put spaces around the equals sign when assigning environment variable values! The shell is very picky about this.
      • Always enclose environment variable values that contain spaces in single or double quotes (see below)
      • Variable names can only contain alphnumeric (A-Z, a-z, 0-9) and underscore ( _ ) characters, and must begin with a letter.

      ...

      Code Block
      languagebash
      foo="My USER name is $USER";  echo $foo   # The textvariable "$USER" is evaluated and its value substituted
      foo="My USER name is ${USER}; echo $foo   # Same as above using longer evaluation syntax
      foo='My
      USER# nameUndefined isenvironment $USER';variables just echoappear $fooas empty  # The text "$USER" is left as-is
      

      Your built-in environment variables (e.g. $USER, $MY_GROUP, $PATH) and their values can be viewed with the env command.

      More at: Intro Unix: Writing text: Environment variables

      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.

      There are three types of quoting in the shell:

      ...

      • 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

      ...

      text
      bar='chess'; echo "Today's game is: $bar"
      unset bar;   echo "Today's game is: $bar"
      
      # Evaluating an environment variable that contains an underscore 
      # may need to use the longer evaluation syntax, if the literal text
      # before or after it is an underscore.
      my_var="middle"
      echo "File name is: foo_${my_var}_bar.txt"
      

      Your built-in environment variables (e.g. $USER, $MY_GROUP, $PATH) and their values can be viewed with the env command.

      More at: Intro Unix: Writing text: Environment variables

      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.

      There are three types of quoting in the shell:

      1. single quoting (e.g. 'some text') – 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 present in the text e.g. newlines (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 and some other metacharacters
        • double quoting also preserves any special characters present 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 syntax $(<expression> ) is equivalent to `<expression>`

      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.

      Single vs double quotes examples:

      ...

      languagebash
        • ) is equivalent to `<expression>`

      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.

      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 examples:

      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 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
      echo "The value of variable 'FOO' is"' "$FOO"'  # Single-quoted text after double-quoted text  inside double quotes
      


      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.

      Backtick evaluation quoting examples:

      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
      
      today=$( date );          echo $today  # environment variable "today" is assigned today's date
      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

      Redirection

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

      ...

      • standard output is stream 1
        • redirect standard output to a file with a the > or 1> operator
          • a single > or 1> overwrites any existing data in the target file
          • a double >> or 1>> appends to any existing data in the target file
      • standard error is stream 2
        • redirect standard error to a file with a the 2> operator
          • a single 2> overwrites any existing data in the target file
          • a double 2>> appends to any existing data in the target file

      If you want output to go to both the Terminal and a file, you can use the tee command (or tee -a to appendgo to both the Terminal and a file, you can use the tee command (or tee -a to append).

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

      More at:   Intro Unix: Writing text: Redirection

      Errors, output and their streams

      ...

      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 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
      # same place as standard out (2>&1)
      #. So data from both standard output and standard error
      # will be written to outerr.log
      ls haiku.txt xxx.txt 1>outerr.log 2>&1 

      More at: