Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Efficient Linux Shell - Shell special characters Summary     - How to install Linux Kernel 4.4 on Ubuntu (Linux)

- MySQL query performance comparison of a single truth (Database)

- How to upgrade to Oracle 11g Oracle 12c (Database)

- Sysdig: system troubleshooting tool (Linux)

- You may not know the Linux command-line network monitoring tool (Linux)

- Linux system find command Detailed (Linux)

- Python 2.7.9 Installation on Linux CentOS 6.6 (Linux)

- To see the Linux device tree (Linux)

- Ubuntu Linux installation GAMIT10.6 (Linux)

- CentOS RedHat YUM Source Extensions Supplement (including 32-bit, 64-bit) (Linux)

- Profile Linux users login shell and login to read (Linux)

- Linux disk quota-related instruction (Linux)

- To generate a certificate using OpenSSL under Linux (Server)

- Jigsaw project will solve the problem of Java JAR hell Mody (Programming)

- Linux Network Security Tips Share (Linux)

- To obtain installation package (RPM) under RHEL6 (Linux)

- Use SecureCRT to transfer files between local and remote hosts (Linux)

- Linux SSH commands (Linux)

- Use SocketServer write a sockets program (Programming)

- Linux System Getting Started Tutorial: How to change the default Java version in Linux (Linux)

  Efficient Linux Shell - Shell special characters Summary
  Add Date : 2016-07-31      
  Under Linux anyway to use shell commands Shell in actual use, the programming experience is very easy to use, but a little difficult are those symbols inside the shell, a variety of special symbols we write Shell when the script if we can use a good, often give us play a multiplier effect, for which Shell will specifically explain the symbols are listed in the form of table for quick lookup. See the table below Oh you know what your Shell sign it?

Shell symbols and various interpretations table:

Shell to use symbols and descriptions
Note symbol (Hashmark [Comments])

! 1. In the beginning of the line shell file as shebang mark, # / bin / bash;

2. elsewhere as a comment on one line, and content after a # will not be executed unless;

3. However, with single / double quotes, # # sign as the characters themselves do not have an annotation role.

As a multi-statement delimiter (Command separator [semicolon]).

Multiple statements on the same line when you want to, you can use semicolons to separate. Note that sometimes need to escape a semicolon.

Continuous semicolon (Terminator [double semicolon]).

In case the option to use the time as a terminator for each option. Bash version 4+ at the time, you can also use [;; &], [; &]

Dot (dot command [period]).

1. The equivalent bash builtin source, such as:

#! / Bin / bash
. Data-file
# Contains data-file;
2. As part of the file name at the beginning of the file name indicates that the file is a hidden file, ls generally do not show up (ls -a can be displayed);

3. As a directory name, a dot represents the current directory, two dots on behalf of the parent directory (the parent directory of the current directory). Note that two or more points does not appear unless you use quotation marks (single / double) surrounded by a dot character itself;

4. The regular expression, dot represents any one character.

Double quotes (partial quoting [double quote]).

Section references. The content can be surrounded by double quotes allow variable expansion, but also allowed the escape character. If the double quotes appear within the string itself need to escape, it is not necessarily double quotes are paired.

Single quotes (full quoting [single quote]).

Single quotes around the content to be treated as a single string, variable inside quotes ban extended, all characters are treated as a character itself (except a single quote itself), single quotes must be paired.

Commas (comma operator [comma]).

1. In connection with a series of mathematical expressions, this string mathematical expressions are evaluated, but only the last evaluation result is returned. Such as:

#! / Bin / bash
let t1 = ((a = 5 + 1, b = 7 + 2))
echo t1 = $ t1, a = $ a, b = $ b
## The $ t1 = $ b;
2. For parameter substitution, and represents the first letter lowercase, and if two commas, then all lowercase, note that this feature in bash version 4 when they were added. example:

a = "ATest"
echo $ {a,}
echo $ {a ,,}
## Front output aTest, behind the output is atest.
Backslash, backslash (escape [backslash]).

1. placed before the special symbols, special symbols escaped role, saying only that a special symbol itself, which commonly used in the string;

2. Directive on the extreme end of a line, followed by a carriage return indicates invalid (in fact, is escaping Enter), enter a new line of successors still as part of the current directive.

Slash, diagonal rod (Filename path separator [forward slash]).

1. as a path separator, the path is only a diagonal bar represents the root directory to the path begins diagonals represents the path from the root directory of;

2. As the operator when the represents the division sign. Such as: a = 4/2

Backquotes backquotes (Command substitution [backquotes]).

Command substitution. This command is enclosed in quotation marks, you can execute commands enclosed, and the result of the execution of the assignment to a variable. Such as: a = `dirname '/ tmp / x.log'`. The results will be returned back dirname assigned to a, note, Mitchell here specially used anti-quotes or single quotes, note the difference.

Colon (null command [colon]).

Air command, which does not do anything, but there is a return value, the return value is 0 (ie: true). The role of this command is very wonderful.

1. do while infinite loop conditions;

2. if the branch as a placeholder (ie, a branch of the time doing nothing);

3. there must be two yuan in local operations as a separator, such as :: $ {username = `whoami`}

4. string variable assignment parameter substitution in redirection (>), and to truncate a file length of 0 (: >> so used, the target exists to do nothing), this can only be Common file and should not be used in pipes, symbolic links, and other special file;

5. You can even be used to annotate (content after the # are not checked, but: after content will be checked if there is a syntax error if the statement, it will error);

6. You can also as a field separator, such as the environment variable $ PATH, or passwd, there are colon as field separator exists;

7. You can also name as a function of the colon, but the original meaning of the colon will change (if you are not careful as the function name, you can use the unset -f: to cancel the defined function).

The exclamation point (reverse (or negate) [bang], [exclamation mark]).

Invert a test result or exit status.

1. Expresses its anti-logic, such as =, this is not equal to the back!;

2. Expresses negated, such as: [! 0-9] ls a # indicates a not immediately behind a number of files;

3. In a different environment inside the exclamation mark can also appear in indirect variable references inside;

4. At the command line, can be used to invoke the command history mechanism, you can try! $,! # Or! -3 See, but to note that this feature can not be used in the script files inside (disabled) .

An asterisk (wildcard / arithmetic operator [asterisk]).

1. As a matching filename extension of a wildcard that can automatically match a document given to each directory;

2. The regular expression can be used as qualifier character, it indicates that the previous match rule to match any number of times;

3. The arithmetic indicates multiplication.

A double asterisk (double asterisk). Arithmetic indicates exponentiation.

The question mark (test operator / wildcard [Question mark]).

1. Expresses its conditional test;

2. Expresses C-style ternary operator ((condition true-result?: False-result)) in double brackets;

3. parameter substitution expression used to test whether a variable set value;

4. As a wildcard, to match the file name extension property, to match any single character;

5. The regular expression, which matches its front rule 0 or 1 times.

The dollar sign (Variable substitution [Dollar sign]).

1. As a preamble variable is used as variable substitution, namely a reference to the contents of a variable, such as: echo $ PATH;

2 is defined as the end of the line (End of line) in a regular expression.

$ {}
Parameter substitution (Variable substitution).

It is used to represent variables in the string.

$ '...'
Citations expand contents escaping execution within single quotes (single quotes as originally cited), octal, hexadecimal value of one or more of the quotes in this way will be within the [\] escaped to expand ASCII or Unicode characters.

$ *

$ @

Position parameter (Positional Parameters).

This script file in use when, in passing parameters will be used when. Both can return all parameters of the call script files, but $ * is all parameters as a whole returns (string), and $ @ is the unit returns each parameter as a parameter list. Note that, when in use need to use double quotes around the $ * $ @ enclose. These two variables affected by $ IFS if in practice, to consider some of the details.

$ #

It represents the number of arguments passed to the script.

This variable value at the time of use, the return value is the exit status of the last command, function, or script, if no error is zero, such as non-zero, it indicates that the last execution prior to this there error.

Process ID variable that holds the current process to run the script ID value.

Parentheses (parentheses).

1, the command group (Command group). By a set of parentheses in the command is a command group, the command group of command really sub-shell (subshell) execution. Because it is run within the sub-shell, and therefore outside the brackets is no way to get the value of the variable in brackets, but in turn, within the command group can get to the outside of the value, this is a bit like the relationship between local and global variables, and in implementation, if you want to hit cd into subdirectories operation, and the operation is completed to return to the current directory, you can consider using the subshell to process;

2. used to initialize the array.

{X, y, z, ...}
Brace expansion (Brace Expansion).

In the command can use this extension to expand the list of parameters, the command will be in accordance with the list of brackets spaced pattern matching extension. Note is that this brace expansion can be no spaces exist, if indeed it is necessary spaces, you must use quotes to be escaped or referenced. Examples: echo {a, b, c} - {\ d, "e", 'f'}

When you add in the Bash version 3 of this brace expansion extension, you can use {A..Z} represents a list of all the characters AZ, this approach extended Mitchell test it, as if only for AZ, az, as well as digital {} minimum .. maximum extension of this approach.

Code block (curly brackets).

This is an anonymous function, but also with different functions in the code block can still access the inside of the variable later in the code block. NOTE: Flowers need braces the inside of a space with a statement separator. In addition, xargs -i in the words, but also as a placeholder text to mark the position of the output text.

{} \;
{} This is a path name, this is not a shell built-in, the situation is now exposed to view, it seems only in the find command inside. Note the semicolon, this is the end of the command sequence find -exec command option, when in actual use, to escape what to avoid being misunderstood shell.

In parentheses (brackets).

1. Test representation, Shell would be tested in the [] within the expression should be noted that, [] is part of the test Shell built-in, rather than using links to external command / usr / bin / test of;

2. In the context of the array, the array representing the elements in square brackets to fill the position of the array element corresponding to the location of the content can be obtained, such as:

Array [1] = xxx
echo $ {Array [1]};
3. Expresses its range of character sets in regular expressions, brackets means that the location can match the character set range.

Double brackets (double brackets).

This structure is also a test, the test [[]] among the expression (Shell keywords). This prevents more than a single script inside the brackets logic errors, such as: &&, ||, <,> operator can be in a [[]] inside the test, but not by []. [[]] There is no file name extensions (filename expansion) or word delimiters (Word splitting), but can command substitution (command substitution) parameter extension (Parameter expansion). No filename characters and delimiters such as blanks. Note that if there appeared octal, hexadecimal, etc., shell performs the conversion automatically compare.

$ [...]
Words extended expression represents an integer (integer expansion).

Perform integer expression in square brackets. example:

a = 3
b = 7
echo $ [$ a + $ b]
echo $ [$ a * $ b]
## Return is 10 and 21
Double parentheses (double parentheses).

It represents an integer extension (integer expansion). The above functions and $ [] the same, but note that $ [] is the return value of the expression inside while (()) is executed, and does not return a value. The latter two executed if the variable value changes, will affect the operation of the subsequent code. Can assign values ​​to variables, variables can be a unary operator operation, it can be two items, the ternary operator.


& <

> &




Redirection (redirection).

scriptname> filename redirects the output of scriptname to file filename to go, if the file exists it is overwritten;

command &> filename command redirects standard output (stdout) and standard error (stderr) to file filename;

command> & 2 of command standard output (stdout) redirected to the standard error (stderr) in;

scriptname >> filename of the scriptname output (with>) filenmae appended to the file, if the file does not exist it is created.

[I] <> filename opens the file filename for reading or writing, and assign the files i for its file descriptor (file descriptor), the file does not exist will be created.



This is the replacement process (Process Substitution).

Note that when in use, and brackets <,> are no spaces between otherwise an error. Its role is somewhat similar path, but and pipelines and some differences in usage, the pipeline as the way a child process to run, this command will have a similar / dev / fd / 63 in / dev / fd / below, / dev / fd / 62 such temporary files, used to transmit data.

Mitchell personal guess the reason for using this method to pass, because the two are not the same before and after the process, and therefore need to share files with a way to transfer data (in fact, say the pipeline should also have the same file?). Some say this is only the Internet to share files only, but after testing, found that although there is / dev / fd / 63 produce such a document, but the document is actually pointing pipe: [43434] link to this channel.

Double less-than sign (here-document [double less then marks]).

This is also known as Here-document, the successor to the contents of stdin redirected to the left command. << Format can save time, and do not command the process easier. In the implementation, when only need to enter the << and the termination identifier, followed (usually after a carriage return) you can enter anything, as long as the input termination flag at the end of the new line, to complete the import data. Use here-document, you can keep the spaces, line breaks and so on. If you want more tidy little shell script, you can put a hyphen between the << and terminator (-).

Three less than the number (here-strings). Here- strings and Here-document similar, here-strings Syntax: command [args] <<< [ "] $ word ["]; $ word expands and as a command of stdin.



Less than, greater-than sign (ASCII Comparison).

ASCII comparison, the comparison is performed ASCII variables, strings? Digital? ... Uh ... this is not ASCII compare it?

\ <... \>
Word delimiters (word boundary).

This is used in the regular expression in a special separator used to mark word boundaries. For example: the matches there, another, them and so on, if only to the match, you can use the word delimiters, \ can only match the the.

Pipe (pipe). Pipeline is Linux, Unix has the concept is very basic, but also a very important concept. Its role is to pipe the output of the previous (left) command generated (stdout) as the pipeline input (right) commands (stdin). Such as: ls | wc l, you can use the pipe commands together. Note: The pipe is the standard output of each process will be used as the standard input of the next command, the standard output during pipeline as a successor can not exceed the standard input command, such as: cat filename | ls -al | sort. Think of this output? At the same time, the pipeline is a child process to run, so the pipeline does not cause variable changes.

> |
Forced redirection (force redirection).

This forces rewrites the file already exists.

And number (Run job in background [ampersand]).

If the command is followed by a & symbol, this command will run in the background. Sometimes, the script in a command running in the background may cause the script to hang, waiting for input, this situation can use the wait command in the original script back to repair.



Logical operators (logical operator).

In the test structure, you can use these two operators to connect two logical values. || When the test condition is true there is a return 0 (true), all false false; && test is when two conditions are true returns True (0), false is false.

Minus sign, hyphen (Hyphen / minus / dash).

1. As an option, the prefix [option, prefix] use. Operator prefix; option flags for the command or filter. Such as:

## COMMAND - [list of options]
ls -al
sort -dfu $ file
set-- $ variable
if [$ file -ot $ file2]
echo "$ file is older than $ file2."
2. The source or destination for the redirect stdin or stdout [dash] when no bunzip2 of tar patches, we can: bunzip2 linux-2.6.13.tar.bz2 | tar xvf -.. The front of the decompressed data as standard input to tar (used here a - representation)

Note: In the implementation of the time, if the file name is [-] at the beginning, and then add this as a directional operator, it may be wrong, and you should file with the appropriate prefix paths to avoid it case occurred, the same, when the echo variable, if the variable is [-] beginning, it may also produce unexpected results, to be safe, you can use double quotes scalar:

var = "- n"
echo $ var
Try ## What output?
Also, this representation is not Bash built-in to achieve this effect at this point, need to see whether the software you use to support this operation;

3. Expresses its previous working directory (previous working directory), so if you cd to a different directory path to replace the previous one, when you can use cd - to achieve their goals, in fact, here [-] use the environment variable $ OLDPWD, attention: here [-] and the former is a little different;

4. The minus sign or minus sign, used in arithmetic operations.

Equal sign (Equals).

1. assignment, assign values ​​to variables, has spaces on both sides of the equal sign;

2. In a comparison test as comparison operators appears here to note that if a comparison appears in brackets, both sides need to have spaces around the equal sign.

Plus (Plus).

1. arithmetic operators, an adder;

2. In the regular expression to indicate that this matching rule before its match at least once;

3. At a command or filter as an option tag, used in some built-in commands or command + to enable certain options, use - to prohibit;

4. Replace the parameter (parameter substitution), the + prefixes substitute value (when the variable is empty, use the latter value +)

Percent (modulo [percent sign]).

1. In arithmetic, this is the modulo operator, namely two numbers remainder after division;

2. Replace the parameters (parameter substitution), it can match a pattern. example:

p = b * 9
var = "abcd12345abc479"
echo $ {var% p}, $ {var %% p}
## From the right look (from left to think about is that symbol?)
## Anything in between b and 9 (inclusive)
## The first one is to find the shortest line with matches
After a ## is to find the maximum consistent with matches (matches greedy?)
Tilde (Home directory [tilde]).

The internal variable $ HOME is the same. The default represents the current user's home directory (home directory), and consistent with the ~ / effect, if the latter tilde with a user name, this indicates that the user's home directory.

~ +
The current working directory (current working directory).

The built-in variable $ PWD and the same.

~ -
Before a working directory (previous working directory).

This consistency and internal variables $ OLDPWD, before [-] is the same.

= ~
Bash has introduced version 3, this is a regular expression matching. Available in [[]] tests, such as:

var = "this is a test message."
[[ "$ Var" = ~ tf * message]] && echo "Sir. Found that." || Echo "Sorry Sir. No match be found."
## You can modify the middle of a regular expression match, regular expressions can, but do not necessarily need to use double quotes.
Caret (caret).

1. In a regular expression, as the first line of the line (beginning-of-line) position identifier;

2. Replace the parameter (Parameter substitution), this usage has two, a caret ($ {var ^}), or two ($ {var ^^}), respectively, the first letter capitalized, all uppercase means (Bash version> = 4).

Whitespace (Whitespace).

Whitespace not just mean spaces (spaces), further comprising tabs (tabs), a blank line (blank lines), or a combination of these types. Can be used as a function of separator, separating commands or variables, blank lines will not affect the behavior of the script, so you can use it to plan script code to increase readability, built in the special variable $ IFS can be used for some commands parameter input split, the default is whitespace. In a string or variable if there is a blank character, you can use quotation marks to avoid possible errors.

How do you know how much is it? Mitchell Shell script at the beginning, it was found that there were not many know about it.

Description: Because it involves the translation of the text content may not be entirely accurate translation, if you find the wrong place, but please bear with correction.
- Unable to start the network after restart clone a virtual machine (Linux)
- Sleuth Kit: used to analyze a disk image and restore files open source forensics tools (Linux)
- Oracle utilized undo data recovery operations (Database)
- Linux batch copy data script (Linux)
- Iptables use examples (Linux)
- MySQL full-index scan bug (Database)
- Linux disk partition, format, mount the directory (Linux)
- Introduction and bash history command to quickly call (Linux)
- Installation and use Docker under Ubuntu 14.04 (Linux)
- Linux and Windows Security Topics (Linux)
- DataGuard Standby backup error RMAN-06820 ORA-17629 to solve (Database)
- Oracle 10g relations with the constraint of column properties NULLABLE (Database)
- CentOS 6.5 platform offline compile and install PHP5.6.6 (Server)
- Docker knowledge base (Server)
- Java Annotation Comments (Programming)
- Linux Security (Linux)
- JSON Introduction and Usage Summary (Programming)
- BCP importing and exporting large amounts of data Practice (Database)
- Install Krita 2.8 on Ubuntu 13.10 / 12.04 / 12.10 (Linux)
- Java regular expression syntax (Programming)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.