Versions Compared

Key

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

...

Code Block
languagebash
titleSplit a command across multiple lines
student01@gsafcomp01:~$ ls haiku.txt \
> mobydick.txt

Notice that the The shell indicates that it is not done with command-line input by displaying a greater than sign ( > ). You can just enter more text then Enter when done.At  At any time during command input you can press Ctrl-c to get back to the command prompt. This is true whether you're entering a single command line prompt or at a > continuation.

For more information, see: Intro Unix: About command line input

Literal characters and metacharacters

...

Most programs/commands read input data from some source, then write output to some destination.

  • A data source can be a file, but can also be the standard input stream.
  • Similarly, a data destination can be a file but can also be a stream such as standard output.

The power of the Linux command line is due in no small part to the power of piping. The pipe operator ( | ) connects one program's standard output to the next program's standard input. Image RemovedThe power of the Linux command line is due in no small part to the power of piping

Image Added

The key to the power of piping is that most Unix commands can accept input from standard input instead of from files. So, for example, these two expressions appear equivalent:

...

  • In more jaberwocky.txt, it is the more command that reads the 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 catprogram reads the file data and writes it to its standard output.
    • the pipe operator ( | ) then connects the cat's standard output from cat to standard input of the more command to more's standard input 
    • 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 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

...

  • 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 (head and tail do not have options to show line numbers)

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

...

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   #   "haiku.txt"  
cat -n haiku.txt | tail -n 7             # display the last 7 lines# ofdisplay "haiku.txt" catcontents -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  with line numbers        
cat -n haiku.txt | tail -n 7          # display the last 7 lines of
     # count lines in "haiku.txt" file
cat haiku.txt | wc -l                              # use wc  "haiku.txt"
cat -ln tohaiku.txt count| linestail of piped-inn text+7  echo 'Hello world!' | wc -c  # display text in "haiku.txt"
       # 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" starting at line 7
cat -n haiku.txt | tail +5 | head -3  # display the middle stanza of
                     # output 3 lines of text using \n to represent newlines

More at:

...

        #   "haiku.txt" (lines 5-7)

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

More at:

What is text?

So what exactly is text? Inside of files, text isn't characters at all – it is all numbers (0's and 1's), because that's all computers know.

On standard Unix systems, each text character is stored as one byteeight binary bits – in a format called ASCII (American Standard Code for Information Interchange). Eight bits can store 2^8 = 256 values, numbered 0 - 255. In its original form values 0 - 127 were used for standard ASCII characters. Now values 128 - 255 comprise an Extended set. See https://www.asciitable.com/

The non-printable ASCII characters we care most about are:

  • Tab (decimal 9, hexadecimal 0x9, octal 0o011)
    • backslash escape: \t
  • Linefeed/Newline (decimal 10, hexadecimal 0xA, octal 0o012)
    • backslash escape: \n
  • Carriage Return (decimal 13, hexadecimal 0xD, octal 0o015)
    • backslash escape: \r

Code Block
languagebash
# display 2 lines of text using \n for newline and \t for Tab
echo -e "aa z\nbb\tcc"                  

# use the hexdump alias to view the hex values for the alphabetic
# and special characters
echo -e "aa z\nbb\tcc" | hexdump

More at:

Other shell concepts

Environment variables

...

Examples:

Code Block
languagebash
foo="My USER name is $USER";  echo $foo   # The variable "$USER" is evaluated and its value substituted
foo="My USER name is ${USER};$USER";  echo $foo   
# Same as above using longer evaluation syntax
foo="My USER name is ${USER}; echo $foo  

# Undefined environment variables just appear as empty 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"

...