Special symbols in shell scripts

Source: Internet
Author: User

1. {} curly braces:
usage One: Wildcard extension
Eg:ls my_{finger,toe}s
This command is equivalent to a combination of the following commands:
ls my_fingers my_toes
eg: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, UserC-data, these several directories
usage Two: can be used for the construction of statement blocks, between statements separated by carriage return. If you want to use multiple statements in certain places where a single statement is used, such as in an and or or list, you can construct a block of statements by enclosing them in curly braces {}.
eg:
{
grep-v "$cdcatnum" $strack _file > $temp _file
cat $temp _file > $strack _file
Echo
cat-n file1
} (Note: The four sentences in the curly braces above are enough to be a block of statements)
usage Three: Parameter extension
${name:-default} uses a default value (usually a null value) in place of the empty or unassigned variable name;
${name:=default} uses the specified value instead of an empty or unassigned variable name;
${name:?message} If the variable is empty or unassigned, the error message is displayed and the execution of the script is aborted and the exit code 1 is returned.
${#name} gives the length of name
${name%word} Removes the smallest part that matches word from the end of name and then returns the remainder
${name%%word} Removes the longest part that matches word from the end of name and then returns the remainder
${name#word} Removes the smallest part that matches word from the head of name and then returns the remainder
${name# #word} Remove the longest part of word from the head of name and return the remainder
(note, name is the variable name, Word is the string to match)
usage Three is useful when dealing with strings and unknown variables.

2, [] brackets:
usage One: Wildcard extension:
allow matching of any single character in square brackets
Eg:ls/[eh][to][cm]*
equivalent to executing ls/etc/home (if there is a/eom directory, it is equivalent to executing ls/etc/home/eom)
Note: cannot be extended under the mkdir command
usage Two: Used for conditional judgment symbols:
The [] symbol can be interpreted as a soft link to the test command, so its usage can be fully referenced by test, and the test location will be replaced with [.
eg:if ["$?"! = 0] equivalent to if test "$?"! = 0
Then echo "executes error"

3, ' command ' anti-quote: ' command ' has the same meaning as $ (command), which returns the result of the current execution command
eg: #!/bin/sh
For file in $ (ls f*.sh);d o
LPR $file
Done
Exit 0
This example implements the name of the file that extended f*.sh gives all matching patterns.

4, ' string ' single quote and ' string ' double quotation marks
double quotation marks: If you want to add a space to a defined variable, you must use single or double quotation marks.
the difference between single and double quotes is that double quotes escape special characters and single quotes do not escape special characters
eg: $ heyyou=home
$ Echo ' $heyyou '
$ $heyyou ($ not escaped)
eg: $ heyyou=home
$ echo "$heyyou"
$ Home (obviously, $ escaped the value of the output of the heyyou variable)

5, the $ number
$# Its role is to tell you how much the total number of reference variables is;
$$ Its role is to tell you the process number of the shell script;
$* Displays all the parameters passed by the script in a single string. Equivalent to $ $ $ ...;
[email protected] is basically similar to $* (see serial number 7), but it is somewhat different when assigning values to an array;
$? The exit code of the previous command;
$-shows the current options used by the shell;
$! The last background run process ID number.

6, $ ((...)) Syntax: Evaluating an expression within parentheses
eg:
#!/bin/sh
x=0
hile ["$x"-ne];d o
Echo $x
x=$ (($x + 1))
Done
Exit 0

7. References to several special parameter variables in the shell
$, $ , $3......${10}, ${11}, ${12} ... : represents each parameter passed in by the script, and note that the number is enclosed in curly braces when it is necessary to represent a parameter after a two-digit number.
[email protected] List all parameters, each parameter is separated by a space
$*: Lists all parameters, separated by the first character of the environment variable IFS

8. List of commands:
and list statement1 && statement2 && statement3 && ...: Executes the last command or list only if all previous commands are executed successfully Statement1 | | Statement2 | | Statement3 | | ...: Allows a series of commands to be executed until one command succeeds, and all subsequent commands are no longer executed
eg:#!/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 list of and lists and or lists are used in logical judgments, and the following is the most common example:
[condition] && command for true | | Command for false: Executes commandfor true when the condition is true, and executes the command for false

9,: Colon: Built-in null instruction, return value is 0
eg: $:
$ echo $?
$0
While : (the statement structure can implement an infinite loop)

10. Semicolon: In the shell, the symbol that functions as a "continuous instruction" is a semicolon .
EG:CD ~/backup; mkdir startup; CP ~/.* startup/.

11.
# #: Indicates that the symbol is followed by the annotation text and will not be executed;
* Match any character in the file name, including the string;
? Matches any single character in the file name.
~ Represents the user's home directory

12, \ Inverted slash: Placed before the command, there is a cancellation of the role of aliases (alias), before the special symbol, the role of the special symbol disappears; placed at the very end of the instruction, indicating that the command is connected to the next line (which makes the carriage return invalid and only acts as a newline)

14,! Exclamation point: Usually it represents the role of anti-logic, such as conditional detection, using! = to represent "Not equal"

15, * * Sub-square operation: Two asterisks represent the meaning of "the second party" in Operation
Eg:let "Sus=2**3″
echo "sus = $sus"

Special symbols in shell scripts

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.