This article is part (iii) of the Linux Shell Series tutorial, more on the Shell Tutorial: Linux Shell Series Tutorials
The shell is an advanced scripting class language and also supports custom variables. Today we will introduce you to the variables in the shell in the relevant knowledge.
To make shell programming more efficient, the system provides some shell variables. A shell variable can hold variable names such as path names, file names, or a number.
The shell considers any of these settings as text strings. There are two kinds of variables, local and environment. There can be 4 strictly, but the other two are read-only and can be considered special variables, which are used to pass parameters to shell scripts.
Defining variables
When defining a variable, the variable name does not have a dollar sign ($), such as:
Variablename= "Value"
Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:
- The first character must be a letter (a-z,a-z).
- You can use an underscore (_) without spaces in the middle.
- Punctuation cannot be used.
- You can't use the keywords in bash (you can see the reserved keywords using the help command).
Examples of variable definitions:
Myurl=http://www.linuxdaxue.com
mynum=100
Using variables
With a defined variable, just precede the variable name with a dollar sign ($), such as:
Your_name= "Linuxdaxue" Echo $your _nameecho ${your_name}
The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:
For skill in Ada coffe Action Java do echo ' I am good at ${skill}script ' done
If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.
It is a good programming habit to add curly braces to all variables.
Redefining variables
A defined variable can be redefined, such as:
Myurl= "http://www.linuxdaxue.com" Echo ${myurl}myurl= "Http://www.linuxdaxue.com" Echo ${myurl}
This is legal, but note that the second assignment can not be written $myUrl = "Http://www.linuxdaxue.com", the use of variables when the dollar symbol ($).
Read-only variables
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable, resulting in an error:
#!/bin/bashmyurl= "http://www.linuxdaxue.com" readonly myurlmyurl= "http://www.linuxdaxue.com"
Run the script with the following results:
/bin/sh:name:this variable is read only.
Delete a variable
Use the unset command to delete a variable. Grammar:
Unset variable_name
The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.
As an example:
#!/bin/shmyurl= "http://www.linuxdaxue.com" unset myurlecho $myUrl
The above script does not have any output.
Show all local shell variables
Use the SET command to display all locally defined shell variables.
Variable type
When you run the shell, there are three different variables:
1) Local Variables
Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
2) Environment variables
All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
3) Shell Special variables
Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation. The following is a description of the special variables in the shell.
Shell Special variables
The special variables in the Shell are mainly as follows:
$ A, $#, $*, [email protected], $?, $$
The following sections describe the variables and meanings and how to use them separately.
Name |
Meaning |
$ |
File name of the current script |
$# |
The number of arguments passed to the script or function. |
$* |
All parameters passed to the script or function. |
[Email protected] |
All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $* and will be discussed separately below. |
$? |
The exit state of the last command, or the return value of the function. |
$$ |
The ID of the current process. For Shell scripts, the process ID where these scripts are located |
$n |
Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is a $ |
Command-line arguments
The arguments passed to the script when the script is run are called command-line arguments. Command-line arguments are represented by a $n, for example, $ = for the first argument, and $ for the second argument, and so on.
and $ A represents the file name of the current script.
The following example illustrates the difference between these parameters:
#!/bin/bashecho "file name: $ A" echo "the first parameter:" echo "the second parameter: $" echo "All parameters: [email protected]" echo "All parameters: $*" echo "parameter number: $#"
Performed by./test.sh Linuxdaxue. com
Execution Result:
File name:./test.sh the first parameter: Linuxdaxue the second parameter:. com all parameters: linuxdaxue.com All parameters: linuxdaxue.com number of parameters: 2
The difference between $* and [email protected]
$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n".
But when they are enclosed in double quotation marks (""), "$*" takes all parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" will separate the parameters to "$1″" ... All parameters are output in the form "$n".
$? Get exit status
$? You can get the exit status of the previous command.
The so-called exit status is the return result after the last command was executed.
Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.
However, there are some commands that return other values that represent different types of errors.
For more shell tutorials See: Linux Shell Series Tutorials
This article fixed link: Linux University network--linux Shell Series Tutorial (c) Shell variables
Linux Shell Series Tutorial (c) Shell variables