# Notes, Shell

### Acknowledge

• What is shell?
• The shell is a program that takes keyboard commands and passes them to the operating system to carry out.
• Almost all Linux distributions supply a shell program from the GNU Project called bash.
• Bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
• What is terminal emulator?
• We use a terminal emulator to interact with the shell when using a GUI.
• KDE uses konsole and GNOME uses gnome-terminal, though it’s likely called simply “terminal” on our menu.
• Shell prompt

• Some Simple Commands

• date - Display the current time and date.
• cal - Display a calendar of the current month.
• df - Display the current amount of free space on your disk drives.
• free - Display the amount of free memory.
• exit - End the terminal session.
• pwd - Print name of current working directory
• cd - Change directory
• ls - List directory contents
• file – Determine file type
• less – View file contents

• Filenames that begin with a period character(.) are hidden.
ls -a can display them, ll -a can display the detailed lists.
• Filenames and commands in Linux, like Unix, are case sensitive. The filenames “File1” and “file1” refer to different files.
• If you want to represent spaces between words in a filename, use underscore characters(_). Though Linux supports long filenames which may contain embedded spaces and punctuation characters: period(.), dash(-), underscore(_).

### Manipulate files and directories

• cp – Copy files and directories
• mv – Move/rename files and directories
• mkdir – Create directories
• rm – Remove files and directories
• ln – Create hard and symbolic links

Here is a useful tip. Whenever you use wildcards with rm (besides carefully checking your typing!), test the wildcard first with ls.
|Wildcard|Meaning|
|:——:|:——|
|*| Matches any characters|
|?| Matches any single character|
|[characters]| Matches any character that is a member of the set characters|
|[!characters]| Matches any character that is not a member of the set characters|
|[[:class:]]| Matches any character that is a member of the specified class|

Character Class Meaning
[:alnum:] Matches any alphanumeric character
[:alpha:] Matches any alphabetic character
[:digit:] Matches any numeral
[:lower:] Matches any lowercase letter
[:upper:] Matches any uppercase letter
• a hard link may not reference a file that is not on the same disk partition as the link itself.
• a hard link may not reference a directory.
• a hard link is indistinguishable from the file itself when listed with ls.
• when a hard link is deleted, the link is removed but the contents of the file itself continue to exist (that is, its space is not deallocated) until all links to the file are deleted.
• a file pointed to by a symbolic link is also written, if you write some something to the symbolic link.
• however when you delete a symbolic link, only the link is deleted, not the file itself.

### Use commands

• type – Indicate how a command name is interpreted
• which – Display which executable program will be executed
• man – Display a command’s manual page
• apropos – Display a list of appropriate commands
• info – Display a command’s info entry
• whatis – Display a very brief description of a command
• whereis - Display the path of the executable program of a command
• alias – Create an alias for a command

### I/O redirection

• cat - Concatenate files
• sort - Sort lines of text
• uniq - Report or omit repeated lines
• grep - Print lines matching a pattern
• wc - Print newline, word, and byte counts for each file
• head - Output the first part of a file
• tail - Output the last part of a file
• tee - Read from standard input and write to standard output and files

• < operator redirects the standard input, > operator redirects the standard output.
>> means to append instead of overwriting.
file streams as standard input, output and error, the shell references them internally as file descriptors zero, one and two, respectively

• to suppress error messages from a command, a special file called /dev/null, and it is a system device called a bit bucket which accepts input and does nothing with it.

• the pipe operator | (vertical bar), the standard output of one command can be piped into the standard input of another.
usually assisted with filters.

• grep is a powerful program used to find text patterns within files.

-i to ignore case, -v to print the lines that do not match the pattern, -n to show the line numbers.

• head prints the first ten lines of a file and the tail command prints the last ten lines by default.

• tee reads standard input and copies it to both standard output (allowing the data to continue down the pipeline) and to one or more files.

### Expansion

• pathname expansion

• arithmetic expansion

• brace expansion

• parameter expansion

• command substitution

#### to control expansion

• double quotes
If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters.

##### bracket expressions

Bracket expressions is used to match a single character from a specified set of characters.

Metacharacters lose their special meaning when placed within brackets. However, two cases have different meanings.
The first is the caret (^), which is used to indicate negation; the second is the dash (-), which is used to indicate a character range.

#### POSIX Character Classes

Character Class Description
[:alnum:] The alphanumeric characters. In ASCII, equivalent to: [A-Za-z0-9]
[:word:] The same as [:alnum:], with the addition of the underscore (_) character.
[:alpha:] The alphabetic characters. In ASCII, equivalent to: [A-Za-z]
[:digit:] The numerals zero through nine.
[:lower:] The lowercase letters.
[:space:] The whitespace characters including space, tab, carriage return, newline, vertical tab, and form feed. In ASCII, equivalent to: [ \t\r\n\v\f]
[:upper:] The upper case characters.
[:xdigit:] Characters used to express hexadecimal numbers. In ASCII, equivalent to: [0-9A-Fa-f]

#### basic regular expressions (BRE) and extended regular expressions (ERE)

What’s the difference between BRE and ERE?
It’s a matter of metacharacters.
With BRE, the following metacharacters are recognized:

All other characters are considered literals.

With ERE, the following metacharacters (and their associated functions) are added:

#### quantifier

The question mark (?) means making the preceding element optional (matching 0 or 1 element).

The asterisk (*) means making the preceding element optional (matching 0 or any number of times elements).

The plus mark (+) means making the preceding element optional (matching 1 or any number of times elements).

The { and } metacharacters are used to express minimum and maximum numbers of required matches. They may be specified in four possible ways:
|Specifier| Meaning|
|:-:|:-|
|{n}| Match the preceding element if it occurs exactly n times.
|{n,m}| Match the preceding element if it occurs at least n times, but no more than m times.|
|{n,}| Match the preceding element if it occurs n or more times.|
|{,m}| Match the preceding element if it occurs no more than m times.|

#### less and vim

They support basic regular expressions.
Pressing the / key followed by a regular expression will perform a search.

### Manipulate text

• cat – Concatenate files and print on the standard output
• sort – Sort lines of text files
• uniq – Report or omit repeated lines
• cut – Remove sections from each line of files
• paste – Merge lines of files
• join – Join lines of two files on a common field
• comm – Compare two sorted files line by line
• diff – Compare files line by line
• patch – Apply a diff file to an original
• tr – Translate or delete characters
• sed – Stream editor for filtering and transforming text
• aspell – Interactive spell checker

#### cat

-A: display non- printing characters in the text.
-n: show line numbers.
-s: suppress the output of multiple blank lines.

#### sort

-n: perform sorting based on the numeric evaluation of a string rather than alphabetical value.
-r: sort in reverse order. results are in descending rather than ascending order.
-b: ignore leading blanks.
-k field1[,field2]: sort based on a key field located from field1 to field2 rather than the entire line.

#### unique

-u: remove duplicates from the sorted output.
-d: only output duplicated lines, rather than unique lines.

#### cut

-f filed_list, -c char_list, -d delimiter_char

#### paste

The paste command does the opposite of cut. Rather than extracting a column of text from a file, it adds one or more columns of text to a file.

#### join

A join is an operation usually associated with relational databases where data from multiple tables with a shared key field is combined to form a desired result.

#### comm and diff

• comm
The comm program compares two text files and displays the lines that are unique to each one and the lines they have in common.
• diff
-c: contex format

-u: unified format

#### patch

To prepare a diff file for use with patch, the GNU documentation (see Further Reading below) suggests using diff as follows:

Once the diff file has been created, we can apply it to patch the old file into the new file:

#### tr

The tr program is used to transliterate characters.

#### sed

-n: no auto-print;

|:-:|:-|
|n| A line number where n is a positive integer.|
|\$| The last line.|
|/regexp/| Lines matching a POSIX basic regular expression. Note that the regular expression is delimited by slash characters. Optionally, the regular expression may be delimited by an alternate character, by specifying the expression with \cregexpc, where c is the alternate character.|
|first~step| Match the line represented by the number first, then each subsequent line at step intervals. For example 1~2 refers to each odd numbered line, 5~5 refers to the fifth line and every fifth line thereafter.|
|addr!| Match all lines except addr, which may be any of the forms above.|

sed Basic Editing Commands
|Command| Description|
|:-:|:-|
|=| Output current line number.|
|a| Append text after the current line.|
|d| Delete the current line.|
|i| Insert text in front of the current line.|
|p| Print the current line. By default, sed prints every line and only edits lines that match a specified address within the file. The default behavior can be overridden by specifying the -n option.|
|q| Exit sed without processing any more lines. If the -n option is not specified, output the current line.|
|Q| Exit sed without processing any more lines.|
|s/regexp/replacement/| Substitute the contents of replacement wherever regexp is found. replacement may include the special character &, which is equivalent to the text matched by regexp. In addition, replacement may include the sequences \1 through \9, which are the contents of the corresponding subexpressions in regexp. For more about this, see the discussion of back references below. After the trailing slash following replacement, an optional flag may be specified to modify the s command’s behavior.|
|y/set1/set2| Perform transliteration by converting characters from set1 to the corresponding characters in set2. Note that unlike tr, sed requires that both sets be of the same length.|

#### aspell

an interactive spelling checker.

### Format text output

omitted

ommited`

### Compile source code

Why compile software? There are two reasons:

1. Availability. Despite the number of precompiled programs in distribution repositories, some distributions may not include all the desired applications. In this case, the only way to get the desired program is to compile it from source.
2. Timeliness. While some distributions specialize in cutting edge versions of programs, many do not. This means that in order to have the very latest version of a program, compiling is necessary.

### Reference

1. 除非注明，本博文即为原创，转载请注明链接地址
2. 本博文只代表博主当时的观点或结论，请不要恶意攻击
3. 如果本文帮到了您，不妨点一下 下面分享到 按钮，让更多的人看到