Variable
Local variable: similar to local variables, only valid for the current Shell Process
Environment variable: applicable to all sub-processes generated by login Processes
Location Parameter: used to pass parameters to shell scripts, read-only
Variable replacement and assignment
The referenced variable value is called variable replacement, and $ is the variable replacement symbol. For example, if a is the variable name, $ A or $ {A} is the variable value.
Assigning a value to a variable name is called a value assignment. The format is variable = value or $ {Variable = value}. If the value contains spaces, you must use ""
Clear the variable value:
Unset variable name
Set Read-Only variables:
Variable = Value
Readonly variable
You can also use declare and typeset to implement
View all read-only variables in the system:
[[Email protected] TMP] # readonly
Declare-ar bash_versinfo = '([0] = "3" [1] = "2" [2] = "25" [3] = "1" [4] =" release "[5] =" x86_64-redhat-linux-gnu ")'
Declare-ir euid = "0"
...
Variable assignment mode
1. Variable :? Value or variable? Value
Displays System Errors for unassigned variable
[[Email protected] TMP] # echo $ {:? Blue}
Bash: A: Blue
[[Email protected] TMP] # A = black
[[Email protected] TMP] # echo $ {:? Blue}
Black
2. Variable: = Value
Assign the value to the variable that is not assigned a value.
[[Email protected] TMP] # A = black
[[Email protected] TMP] # echo $ {A: = blue}
Black
[[Email protected] TMP] # unset
[[Email protected] TMP] # echo $ {A: = blue}
Blue
[[Email protected] TMP] # echo $
Blue
3. Variable:-Value
Returns the value for the variable that is not assigned a value, but the variable value is not assigned.
[[Email protected] TMP] # unset
[[Email protected] TMP] # echo $ {A:-blue}
Blue
[[Email protected] TMP] # echo $ {:? Blue}
Bash: A: Blue
Shell script variables with no type
Shell script variables are non-typed and have both numeric and numeric assignment values. They can be directly used without pre-defined variables. The initial value of numeric type is 0, and the initial value of numeric type is null.
[[Email protected] TMP] # Cat A. Sh
#! /Bin/sh
C = ""
Echo "c = $ C"
Let "C + = 1" # Let command is used to execute arithmetic operations. Let "C + = 1" is equivalent to C + = 1
Echo "c = $ C"
Echo "E = $ e"
Let "e + = 1"
Echo "E = $ e"
E = "hello" # the numeric value of this variable changes to 0 again.
Echo "E = $ e"
Let "e + = 2"
Echo "E = $ e"
[[Email protected] TMP] #./A. Sh
C =
C = 1
E =
E = 1
E = Hello
E = 2
Environment Variable
The environment variables of the parent process can be passed to the child process, but the environment variables of the child process (or the environment variables defined by the parent process are modified) cannot be passed or affect the parent process.
Define environment variables:
Environ-variable = Value
Export environ-Variable
Common environment variables are capitalized.
Export indicates that this variable is an environment variable.
Clear environment variables:
Unset environ-Variable
Important environment variables:
1. pwd and oldpwd
PWD records the current directory, and oldpwd records the old working directory
[[Email protected] TMP] # echo $ pwd
/Tmp
[[Email protected] TMP] # cd
[[Email protected] ~] # Echo $ pwd
/Root
[[Email protected] ~] # Echo $ oldpwd
/Tmp
2. Path
Shell searches for the directory list in path for each input command.
[[Email protected] ~] # Echo $ path
/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin:/usr/x11r6/bin:/root/bin
[[Email protected] ~] # Export Path = $ path:/tmp/bin # Add a new path
[[Email protected] ~] # Echo $ path
/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin:/usr/x11r6/bin:/root/bin:/tmp/bin
3. Home
Path of the user's home directory
[[Email protected] ~] # Echo $ home
/Root
4. Shell
Save the default shell value (/bin/bash)
5. User and uid
The name and ID of the logged-in user
[[Email protected] ~] # Echo $ user $ uid
Root 0
6. ppid and $
$ Ppid: the parent process ID of the current process. $ it is the process ID of the current process.
7. PS1 and PS2
The START prompt and branch prompt respectively (that is, the prompt when the command is input in multiple lines)
[[Email protected] ~] # Echo $ PS1
[\ [Email protected] \ H \ W] \ $
[[Email protected] ~] # Echo $ PS2
>
\ U represents the current user name
\ H indicates the Host Name
\ H Represents the Host Name and domain name
\ W indicates the name of the current working directory
\ W indicates the complete path of the current working directory
\ $ If the UID is 0, # is displayed; otherwise, $ is printed.
8. Ifs
Specifies the input domain Separator of shell. The default Delimiter is space.
[[Email protected] ~] # Export ifs = ""
[[Email protected] ~] # Echo $ path
/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin:/usr/x11r6/bin:/root/bin:/tmp/bin
[[Email protected] ~] # Export ifs =:
[[Email protected] ~] # Echo $ path
/Usr/Kerberos/sbin/usr/Kerberos/bin/usr/local/sbin/usr/local/bin/sbin/bin/usr/sbin/usr/bin/usr/x11r6 /bin/root/bin/tmp/bin
Environment variable configuration file
Hide a file in the root directory of the user
1. bash_profile
The user logs on automatically and initializes the environment variable. If the file does not exist, the user executes the/etc/profile command.
You can add new environment variables and execute. bash_profile or source. bash_profile to take effect immediately (you can also log out)
The source command is equivalent to the. Command
Directly execute the file to start a sub-shell, and the environment variables set by the sub-shell script cannot affect the current shell script. the soure command (or. Command) allows the short script to be executed in the current shell.
[[Email protected] ~] # Cat. bash_profile
#. Bash_profile
# Get the aliases and functions
If [-f ~ /. Bashrc]; then
.~ /. Bashrc
Fi
# User specific environment and startup programs
Path = $ path: $ home/bin
2. bashrc
Sets the environment variables of the sub-shell to separate the environment variable settings during user login from the environment variable settings of the sub-shell, improving flexibility.
3. bash_logout
When you log out, you can write commands that identify certain environment variables or do not exist.
Export path
Unset Username
Parent process, child process environment variable relationship
The child process can inherit the environment variables of the parent process, but the environment variables modified by the child process are only valid for the child process and do not affect the environment variables of the parent process, the parent process of the Environment Variable created by the child process cannot receive it, while the local variable does not affect each other.
Example:
[[Email protected] TMP] # Cat father. Sh
#! /Bin/bash
Fatherlocal = "fatherlocal"
Fatherenviron = "fatherenviron"
Export fatherenviron
Echo "in fahter. Sh"
Echo "Father process ID is $"
Echo "fatherlocal is $ fatherlocal"
Echo "fatherenviron is $ fatherenviron"
$ PWD/child. Sh
Echo "return to Father. Sh"
Echo "fatherlocal is $ fatherlocal"
Echo "fatherenviron is $ fatherenviron"
[[Email protected] TMP] # Cat child. Sh
#! /Bin/bash
Echo "in child. Sh"
Echo "child process ID is $ and my father is $ ppid"
Echo "fatherlocal is $ fatherlocal"
Echo "fatherenviron is $ fatherenviron"
Echo "Change faterenviron"
Export "fatherenviron = redefinfatherenviron"
Echo "New fatherenviron is $ fatherenviron"
Result:
[[Email protected] TMP] #./father. Sh
In fahter. Sh
Father process ID is 29171
Fatherlocal is fatherlocal
Fatherenviron is fatherenviron
In child. Sh
Child process ID is 29172 and my father is 29171
Fatherlocal is
Fatherenviron is fatherenviron
Change faterenviron
New fatherenviron is redefinfatherenviron
Return to Father. Sh
Fatherlocal is fatherlocal
Fatherenviron is fatherenviron
Location parameters
$0 Script Name
$1 first Parameter
$ {10} the tenth parameter, starting from 10 to add {}
$ # Number of parameters
$ * Or [email protected] All parameters, excluding $0
$ Process ID
$ Ppid parent process ID
$? Exit status. "0" indicates no error, and "non-0" indicates an error.
Reference
Reference is to block the special meaning of a special character and interpret it as literal meaning.
"" References all characters except the dollar sign ($), backquotes (''), and backslash (\)
''References all characters''
''. Shell interprets the content in the quotation marks as a system command.
\ Backslash to block the special meaning of the next character
You can retain multiple spaces (with line breaks) for variables with double quotation marks instead of the field separator (IFS is empty by default). Double quotation marks prevent variables from changing separators.
Example:
[[Email protected] TMP] # A = "a B C"
[[Email protected] TMP] # echo $
A B C
[[Email protected] TMP] # echo "$"
A B C
[[Email protected] TMP] # echo '$'
$
[[Email protected] TMP] # echo 'Why can't I'm you're'
Why cant IM you are
[[Email protected] TMP] # echo "Why can't I'm you are"
Why can't I'm you are
Command replacement
Replace the standard output of a command with the location of the command.
Back quotes ('') and $ ()
$ () Commands can be nested
$ () Is \, and '(') is interpreted as a space.
The result of execution in other languages can be assigned to shell variables using reverse quotation marks, so as to be processed by shell.
Example:
[[Email protected] TMP] # echo \\
\
[[Email protected] TMP] # echo $ (echo \\)
\
[[Email protected] TMP] # echo 'echo \\'
# Blank lines
Command 'echo '# command represents any command, which is equivalent to no Parameter
Command "'echo '" # null string parameter
Command 'echo x y' # contains two parameters: X and Y.
Command "'echo x y'" # With a parameter, X Y
Escape
Purpose:
1. Remove the meaning of some special characters
2. A second-level prompt appears for a single \ in the command line, which is also applicable in shell scripts.
3. Use echo, sed, awk, and other commands to make some letters express special meanings.
1.
Variable = "() \\{}\$ \""
Echo $ variable
Echo "$ variable"
Ifs = '\' # change the separator of input variables
Echo $ variable
Echo "$ variable" # Double quotation marks prevent variables from changing the separator and retain the variable separator.
[[Email protected] TMP] #./a3.sh
() \ {}$"
() \ {}$"
() {}$"
() \ {}$"
2.
2.1
[[Email protected] TMP] # echo \
> Hello World
Hello World
2.2
[[Email protected] TMP] # echo \ # You can use "or" Echo ".
> "Hello World"
Hello World
2.3
[[Email protected] TMP] # echo "\" # note that there is an essential difference between this and the previous two. Here, the \ escape "causes missing" command to end after completion, same as 2.4
> Hello World"
"
Hello World
2.4
[[Email protected] TMP] # echo"
> Hello
> World"
Hello
World
2.5
[[Email protected] TMP] # variable = \
> Hello World
Bash: World: Command not found # assign values to variables using quotation marks if spaces exist.
2.6
[[Email protected] TMP] # variable = \
> "Hello World"
[[Email protected] TMP] # echo $ variable
Hello World
2.7
[[Email protected] TMP] # Cat a5.sh
#! /Bin/bash
Echo \ # single/also applies in shell scripts
Hello World
[[Email protected] TMP] #./a5.sh
Hello World
3.
Special meanings of letters followed:
\ N new row
\ R return
\ T Tab key
\ V or \ f line feed, but the cursor stays at the original position (with the same horizontal coordinates)
\ B Return key
\ A sends an alarm
Characters corresponding to \ 0xx ASCII code 0xx
Example:
[[Email protected] TMP] # echo Hello \ B \ 101
Hellob101
[[Email protected] TMP] # echo $ 'Hello \ B \ 000000' # is basically equivalent to-e"
Hella
[[Email protected] TMP] # echo-e "Hello \ B \ 0101" # note that 0 of \ 0101 cannot be parsed as ASCII code without 0
Hella
[[Email protected] TMP] # echo-e "Hello \ B \ 101"
Hell/101
[[Email protected] TMP] # echo-e Hello \ B \ 101 # Note-E is not "" and cannot be parsed
Hellob101
Note:
Echo [Option] [String]
Option-e indicates that special characters formed after the escape character (\) are interpreted as special characters.
Option-N indicates no line feed after the output text:
[[Email protected] TMP] # echo-n "hello"
Hello [[email protected] TMP] #
This article is from the "flyclc" blog, please be sure to keep this source http://flyclc.blog.51cto.com/1385758/1540166