The meaning of shell variable $#,$@,$0,$1,$2 in Linux/shell ${}, # #和 percent use example/export__linux

Source: Internet
Author: User
The meaning of shell variable $#,$@,$0,$1,$2 in Linux is explained:
Variable Description:
$$
The shell itself PID (ProcessID)
$!
The PID of the Shell's last running background process
$?
End code of the last command to run (return value)
$-
Flag list using the SET command
$*
A list of all parameters. Output all parameters in the form of "$ $n", as in the case of "$*" with "".
$@
A list of all parameters. such as "$@" with "" in the case, to "$ $" ... Output all parameters in the form of "$n".
$#
Number of arguments added to the shell
$
File name of the shell itself
$1~ $n

The value of each parameter added to the shell. $ is the 1th parameter, $ is the 2nd parameter ....

-----------------------------------------------------------------

In this paper, we introduce the ${}, # #和 percent usage example in the shell, and give the results in different situations.
Suppose a variable is defined as:
The code is as follows:
File=/dir1/dir2/dir3/my.file.txt
You can replace each of the different values with ${}:
${file#*/}: Delete the first/and left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Delete the last/and left string: my.file.txt
${file#*.} : Delete the first one. And the string to the left: file.txt
${file##*.} : Delete the last one. And the string to the left: txt
${file%/*}: Delete the last/And right string:/dir1/dir2/dir3
${file%%/*}: Delete the first/and the right string: (null value)
${file%.*}: Delete the last one. And the string to the right:/dir1/dir2/dir3/my.file
${file%%.*}: Delete the first one. And the string to the right:/dir1/dir2/dir3/my
The method of memory is:
# is to remove the left (on the keyboard # on the left of the $)
% is to remove the right (on the keyboard% on the right side of the $)
A single symbol is a minimum match; two symbols is the maximum match
${file:0:5}: Extract leftmost 5 bytes:/dir1
${file:5:5}: Fetches 5 consecutive bytes to the right of the 5th byte:/dir2
You can also replace the string in the value of the variable:
${file/dir/path}: Replaces the first dir with the Path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Replace all dir with Path:/path1/path2/path3/my.file.txt

//------------------------------------------------------------------------------------------------------

1, executes the script is in a child shell environment to run, after the script executes the child shell to exit automatically;

2. How the system environment variables in a shell are copied into the sub shell (variables defined with export);

3. The system environment variable in a shell is only valid for the shell or its child shell, and the variable disappears at the end of the shell (and cannot be returned to the parent shell).

4. A variable that is not defined by export is valid only for the shell, and the child shell is not valid.

---------------------------------------

-D: Determine whether the set is a directory
-Z: Determine if a variable exists with a value
-F: Judge whether a file is made
-L: Determine if a symbolic link is made
-R: Determining whether the made is readable
-S: Determine if the existing object length is 0
-W: Judge whether the formulated can be written
-X: Determines whether an existing object can be executed
。 : Negative sign of test condition
These file operations are often convenient for scripting, especially if they are used in an IF condition statement

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.