Shell variables
define
- A variable is, as the name suggests, a quantity that can be changed
- A variable must start with a letter or underscore. The name must contain only digits, letters, or underscores
- The variable name contains a maximum of 255 characters
- The variable name must be unique within the valid range
- The default variable type is string
classification
- string
- The integer
- Floating point Numbers
- The date type
User-defined variables
- The values of these variables are self-defined
- The variable name cannot start with a number
- No Spaces on either side of the equal sign
Define variables
name=wanghaoyu
age=23
Copy the code
The output variable
echo name # wanghaoyu
Copy the code
Note that variable types are defined as strings by default.
x=1
y=2
z=3
$o=$x+$y+$z
echo $o # 1+2+3
Copy the code
Reference variables during assignment
There are two ways to refer to a variable during assignment
-
m="$x"2
-
m=${x}2
Both methods mean assigning a value to the variable m, which is the value of the variable x plus the string 2.
set
Command to view the variables that have been set
set
Command to query all variables that have taken effect by default, including system variables and user-defined variables.
set | grep name
unset
unset
Delete variable
unset a
The environment variable
- Environment variables are global, while custom variables are local
- Custom variables will only be current
shell
While environment variables can take effect in the currentshell
And the sonshell
In effect - These variables mainly hold data related to the operating system environment
- The variable can be defined by itself, but the name and function of the variable in effect for the system are fixed
Subshells can share environment variables, but different Windows cannot access environment variables of another shell, also temporary, other shell Windows cannot access.
Customize environment variables
- use
export
The command defines environment variables
Export Variable name = variable valueCopy the code
env
env
andset
The difference is,set
You can view all the variablesenv
Only set environment variables can be viewed.
env | grep name
Common environment variables
The variable name | meaning | The sample |
---|---|---|
HOSTNAME | The host name | localhost |
SHELL | Current SHELL type | SHELL=/bin/bash |
HISTSIZE | Number of historical commands | HISTSIZE=1000 |
SSH_CLIENT | Currently, if the SSH link is used, the client IP (the IP address of the link) is recorded. | SSH_CLIENT = 192.168.1.95 22 18011 |
USER | Currently logged in user | USER=root |
PATH
System search path
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Copy the code
The role of $PATH was previously mentioned in the PATH environment variable.
If we enter an ls command, the system will search for the corresponding command in the $PATH directory from left to right, and then execute it. The command does not exist if it is not found.
When we want to make a script executable via global commands, we can do so in the following ways:
The same is true with nodeJS.
- Add executable permission to the corresponding file.
The beginning of the file indicates the environment in which the file is run. For example, the shell needs to start with #! /bin/bash, node file #! /usr/bin/node tells the system that this is a node to file.
- We can add our corresponding executable file to any directory in the environment variable, and then we can use it directly through the command. (
bash
You can execute the script directly by calling the command with the file name.
Add the command script to the directory of the existing environment variable $PATH.
- And of course we can be here
$PATH
And put the command script into this new file. Pay attention to:
Partition, this time can also achieve the same effect.
By redefining the environment variable export $PATH=”$PATH:[new directory PATH]”.
Linguistic environment variable
-
Query current language
-
In Linux, you can use local to set up the different languages in which your program will run. Local support is provided by ANSI C.
-
The local command rule is < language >_< locale >.< string encoding set >, for example, zh_CN. Utf-8.
-
The LANG system defines variables in the host language
Position parameter variable
- This type of variable is used to pass parameters or data to the script, the variable name cannot be customized, the function of the variable is fixed.
Position parameter variable | role |
---|---|
$n |
n As the number,n Represents the command itself,$1 to $9 Parameter number of the commandn Three parameters, 10 or more are required${n} , such asThe ${10} |
$* |
This variable represents all the arguments in the command,$* I think of all the parameters as a whole |
$@ |
This variable also represents all the arguments in the command, but$@ Each parameter is distinguished separately |
$# |
Represents the number of all parameters in the command linelength |
$n
The example test. Sh
#! /bin/bash
echo "$1+$2+$3"
Copy the code
sh test.sh 1 2 3
Output 1 + 2 + 3
$@
For example the for sh
#! /bin/bash
for i in "$@"
do
echo "i=$i"
done
Copy the code
sh for.sh 1 2 3
Copy the code
i=1
i=2
i=3
Copy the code
The concept of $@ is similar to arguments in JS, which can be obtained one by one through a loop.
$*
For example the for sh
#! /bin/bash
for i in "$*"
do
echo "i=$i"
done
Copy the code
i=3 4 5
Copy the code
The difference between $@ and $* is that each parameter is treated separately, or all parameters are treated as a whole.
$#
Number of parameters
echo $#
Copy the code
sh hello.sh 1 2 3 4 5
Copy the code
5 # The parameter length is 5Copy the code
Predefined variable
- Predefined variables are defined in scripts. They cannot be customized and have fixed functions.
Predefined variable | role |
---|---|
$? |
Status of the last command executed. 0 indicates success, 2 indicates failure |
$$ |
Indicates the process ID of the current process |
When we need to determine a branch condition to run based on the return of the command, then $? It’s very useful.
read
The command
Read [option] [variable name]
Indicates that the value entered by the user can be read and stored in the variable name.
options | meaning |
---|---|
-p |
Prompt message, waitingread A prompt message is displayed during input |
-t |
Number of seconds,read By default, the command waits for the user to input the command-t Parameter allows you to specify the wait time |
-n |
Number of characters,read If the command accepts only the specified number of characters, it is executed. (Specified number of words) |
-s |
Hide input data, usually used for password entry |
#! /bin/bash read -p 'Your name is' -t 5 name echo -e '\n' read -p' Your gender is :' -n 1 gender echo -e '\n' read -p 'Your password is' -s password Echo $name,$gender,$password #Copy the code
The read command is often seen in things like logging in to mysql.