Instructions for variable usage in a shell in Linux

Source: Internet
Author: User
Tags arrays chmod echo name


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 ....

Let's write a simple script that interprets the meaning of each variable after execution.

# Touch Variable
# VI Variable
1
2 # Touch variable# VI Variable

The script reads as follows:

Shell
#!/bin/sh
echo "number:$#"
echo "Scname:$0"
echo "A:"
echo "Second:$2"
echo "argume:$@"

#!/bin/shecho "number:$#" "echo" Scname:$0 "echo"--"echo" Second:$2 "echo" argume:$@ "

Save exit

Give script Execute permissions

# chmod +x Variable
1 # chmod +x variable
Execute script:

#./variable AA BB
1 #./variable AA BB
Results:

Number:2
Scname:./variable
First:aa
Second:bb
ARGUME:AA BB

You can see by displaying the results:
$# is the number of parameters passed to the script
$ is the name of the script itself
$ is the first argument passed to the shell script
$ is the second argument passed to the shell script
$@ is a list of all parameters passed to the script

Add a little bit about shell variables


Name is a variable name, value is a variable, such as NAME=ZDD, note that the shell defines the variable without the need for $, while the use of the variable will need to add $, which is different from Perl, Perl defined variables also need the $ symbol. The following points should be noted when defining variables.

A variable in shell can only consist of letters, numbers, and underscores, and cannot begin with a number.
• If the value of a variable contains spaces, enclose it in quotation marks (either single quotes or double quotes), such as "Hello World" or ' Hello World '.
• There are no spaces around the equals sign.
Using variables
When accessing a variable, just add a $ to the front of it, such as Echo $name, and if not $, display the name of the variable, that is, echo name will display name.

Array variables
The Bourne shell (SH) supports only scalars, Korn Shell (Ksh) and the second and later versions of Bourne Again Shell (bash) support array variables.

Defining arrays
Assigning values to individual elements
Name[index]=value

Assigning values to an entire array
Bash

Name= (value1 value2 ... valuen) each element is separated by a space.

You can also assign values to only certain elements

Name= ([0]=value0 [3]=value3 [5]=value5]

Ksh

Set-a name value1 value2 ... valuen, name is an array name.

Using arrays
Accessing a single value
${name[index]}

Accessing the entire array
${name[*]}

${name[@]} can handle array elements that contain spaces and is recommended for use.

Read-only variable
Defining methods

Name=value

ReadOnly Name

Once a read-only variable is defined, its value cannot be changed or deleted with unset.

Variable type
Local variables
A variable that is customized by the user in a shell script.

Environment variables
Use the Export command to view environment variables for the current system.

Shell variables
The common shell variables are as follows.

Pwd

Uid

Shlvl

REPLY

RANDOM

SECONDS

Ifs

PATH

Home

Delete a variable
unset name

How do I delete an array variable?

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.