Special characters in Linux Shell

Source: Internet
Author: User
Special characters in Linux Shell

1. braces:

Usage 1: wildcard Extension

Eg: ls my _ {finger, toe} s

This command is equivalent to a combination of the following commands:

Ls my_fingers my_toes

For example: mkdir {usera, userb, userc}-{home, bin, data}, we will get usera-home, usera-bin, usera-data, userb-home, userb-bin, userb-data, userc-home, userc-bin, and userc-data. These directories are used in statement block construction and separated by carriage return. If you want to use multiple statements in some places where a single statement is used (for example, in the and or list), you can enclose them in braces {} to construct a statement block.

Eg:

{

Grep-V "$ cdcatnum" $ strack_file> $ temp_file

Cat $ temp_file> $ strack_file

Echo

Cat-N file1

} (Note: The four statements in the braces above can be a statement block)

Usage 3: parameter Extension

$ {Name:-default} uses a default value (usually a null value) to replace the names of null or unassigned variables;

$ {Name: = default} uses the specified value to replace the empty or unassigned variable name;

$ {Name :? Message} if the variable is null or has not been assigned a value, an error message is displayed, the script is aborted, and exit code 1 is returned.

$ {# Name} indicates the name length.

$ {Name % word} deletes the minimum part that matches the word from the end of the name, then, the remaining part $ {name % word} is returned. The longest part that matches the word is deleted from the end of the name, then, the remaining part $ {name # Word} is returned. The minimum part that matches the word is deleted from the name header, then, return the remaining part $ {name # Word}. Delete the longest part that matches the word from the name header, and then return the remaining part (note, name is the variable name, word is the string to be matched)

Usage 3 is useful when processing strings and unknown variables.

2. [] brackets:

Usage 1: wildcard extension:

Allows matching of any single character in square brackets

Eg: ls/[EH] [to] [cm] *

It is equivalent to executing ls/etc/Home (if there is a/EOM directory, it is equivalent to executing ls/etc/home/EOM)

Note: The mkdir command cannot be expanded.

Usage 2: used for condition judgment symbols:

The [] symbol can be understood as a soft link to the test command. Therefore, you can replace the test position with [by referring to test.

Eg: If ["$? "! = 0] is equivalent to If test "$? "! = 0

Then ECHO "executes error"

3. 'command' quotation marks:The 'command' and $ (command) both have the same meaning and return the result of the current command execution. For example :#! /Bin/sh for file in $ (LS f *. Sh); do

LPR $ File

Done

Exit 0

In this example, the extension f *. Sh is used to show the names of all files matching the pattern.

4. 'string' single quotation marks and "string" Double quotation marks

Double quotation marks: to add spaces to the defined variables, you must use single quotation marks or double quotation marks,

The difference between Single and Double quotation marks is that double quotation marks escape special characters while single quotation marks do not escape special characters.

Eg: $ heyyou = home

$ Echo '$ heyyou'

$ Heyyou ($ no escape)

Eg: $ heyyou = home

$ Echo "$ heyyou"

$ Home (obviously, $ escaped the output value of the heyyou variable)

5. $ # It is used to tell you the total number of referenced variables;

$ It indicates the process Number of the shell script;

$ * Display all parameters passed by the script with a single string. Equivalent to $1 $2 $3 ...... $ @ is similar to $ * (see sequence number 7), but it is somewhat different when assigning values to arrays. $? Exit code of the previous command;

$-Current options used by shell are displayed;

$! The ID of the last process running in the background.

 6. $ (...) Syntax: Evaluate the expressions in parentheses

Eg:

#! /Bin/sh

X = 0

Hile ["$ X"-ne 10]; do

Echo $ x

X = $ ($ x + 1 ))

Done

Exit 0

7. Reference of several special parameter variables in Shell

$1, $2, $3 ...... $ {10}, $ {11}, ${12 }...... : Each parameter passed in by the script. Note that the number must be enclosed in curly brackets when it is required to represent parameters after two digits.

$ @ List all parameters separated by spaces $ *: list all parameters. Each parameter is separated by the first character of the environment variable IFS.

8. Command list:

And list statement1 & statement2 & statement3 &&... : The last command is executed only when all the preceding commands are successfully executed.

Or list statement1 | statement2 | statement3 |... : You can execute a series of commands until a command is successful. After that, all commands are no longer executed. For example :#! /Bin/sh

Touch file_one

Rm-F file_two

If [-F file_one] & Echo "hello" & [-F file_two] & Echo "there"

Then

Echo "in if"

Else

Echo "in else"

Fi

Exit 0

The output of the above example is:

Hello

In else

The and list and or list are used in logical judgment. The following is a common example:

[Condition] & command for true | command for false:

If the condition is true, run commandfor true. If the condition is false, Run Command for false.

9. colon (:): Empty built-in command. The returned value is 0.

Eg: $:

$ Echo $?

$0

While: (this statement structure can implement an infinite loop)

 10.; semicolon:In shell, the symbol used for the "continuous command" function is "Semicolon" eg: Cd ~ /Backup; mkdir startup; CP ~ /. * Startup /.

11. # Well no.: The symbol is followed by the annotated text and will not be executed. * It matches any character in the file name, including a string ;? Match any single character in the file name.

~ Represents the user's home directory

12. \ backslash: Put it before the command to cancel the aliases (alias); put it before the special symbol, then the role of the special symbol disappears; put it at the end of the command, indicates connecting the next line of the command (the carriage return is invalid and only applies to line breaks)

13 ,! Exclamation point: Usually it represents the role of anti-logic. For example, it is used in condition detection! = To indicate "not equal"

14. ** Power Operation: The meaning of the two asterisks in the computing age

Eg: Let "Sus = 2 ** 3"

Echo "Sus = $ sus"

$ Sus = 8-

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.