One, variable operation
A. variable display, print
Syntax: Echo $var
B. Variable settings
Syntax: Var=value
Setting rules for variables:
1. Variable ends cannot be directly connected to spaces
2. Variable names can only be letters and numbers, but the beginning does not make the numbers
3. Double quotes within the special character Furu $ etc, keep original properties
Copy Code code as follows:
[Root@bogon ~]# var= "Lang is $LANG"
[Root@bogon ~]# Echo%var
Root@bogon ~]# Echo $var
The Lang is zh_cn. UTF-8
1. Special characters within single quotes are generic characters only
Copy Code code as follows:
[Root@bogon ~]#
[Root@bogon ~]# var= ' lang is $LANG '; Echo $var
The Lang is $LANG
2. In a sequence of commands, also need to provide information through other commands, can be used in single quotes ' command ' or & (command), for example: instruction 1 in the process of execution need to know the value of the instruction 2, but the instruction 1,2 in a series of instructions
Copy Code code as follows:
[Root@bogon ~]# Uname-r
2.6.18-371.el5
[Root@bogon ~]# cd/lib/modules/$ (uname-r)/kernel
[Root@bogon kernel]#
3. The accumulation of variables
Copy Code code as follows:
[Root@bogon kernel]# Var=${var}yes
[Root@bogon kernel]# Echo $var
The Lang is $LANGyes
4. Array variable setting and reading
Copy Code code as follows:
[Root@bogon ~]# Array[1]=a
[Root@bogon ~]# Array[2]=b
[Root@bogon ~]# Array[3]=c
[Root@bogon ~]# echo ${array[1]}
A
[Root@bogon ~]# echo ${array[2]}
[Root@bogon ~]# echo ${array[3]}
C
C. Cancellation of variables (unset)
Syntax: unset var
D. Variable view (SET)
Syntax: Set
Some of the more important custom variables
Histfile: History Storage Location
MailCheck: How many seconds to scan the mailbox to see if there are any new messages
PS1: Prompt settings
$: Now this Shell's PID
?: The return code that just completed the command. 0 is correct, non 0 is wrong
Example:
Copy Code code as follows:
Some of the more important custom variables
Histfile: History Storage Location
MailCheck: How many seconds to scan the mailbox to see if there are any new messages
PS1: Prompt settings
$: Now this Shell's PID
?: The return code that just completed the command. 0 is correct, non 0 is wrong
E. Variable keyboard reads (read)
Syntax: Read [-pt] Var
Options and Parameters:
-P: To follow the prompts
-T: followed by a description of the wait input
Example:
Copy Code code as follows:
[Plain] View plaincopyprint?
[Root@bogon ~]# Read atest
This is a test
[Root@bogon ~]# Echo $atest
This is a test
[Root@bogon ~]# read-p "Please input ..." attest
Please input ... Hello World => Tip Information
[Root@bogon ~]# Echo $atest
Hello World
[Root@bogon ~]# read-p "Please input ..."-T 5 atest
Please input ... =>5 seconds not entered back to command line mode
[Root@bogon ~]#
[Root@bogon ~]# Echo $atest
Hello World
F. Variable declaration (declare)
Syntax: Declare [-AIXR] Var
Options and Parameters
Declare does not pick up any content, represents query all variables, the role and set consistent
-A: Defines the variable named variable as an array type
-I: Defines the variable named variable as an integer number (integer) type
-X: Use the same as export, which is to turn the variable of the rear into an environment variable;
+x: Change an environment variable to a custom variable
-r: The variable is configured to be a readonly type, the variable cannot be changed, and cannot be unset (log off and then log back in order to change)
Example:
Copy Code code as follows:
[Plain] View plaincopyprint?
[Root@bogon ~]# Echo $sum
100+50+10 => Default As String handling
[Root@bogon ~]# declare-i sum=100+50+10
[Root@bogon ~]# Echo $sum
160 => declared as int so it can be additive.
[Root@bogon ~]# Declare-x sum
[Root@bogon ~]# Export | grep sum
Declare-ix sum= "160" => query to be environment variable
[Root@bogon ~]# declare +x sum
[Root@bogon ~]# Export | grep sum= > query is not an environment variable
[Root@bogon ~]# declare-r sum;sum=test
Bash:sum:readonly variable => read-only allow modification
G. Variable content deletion
Grammar
${var#/key}: Remove the one that matches the shortest key in the past
${var##/key}: Delete the one that matches the key longest in the past
${var%/key}: Deletes the one with the shortest key from the back
${var%%/key}: Deletes the one with the shortest key from the back
Example: ${var#/key}
Copy Code code as follows:
[Plain] View plaincopyprint?
[Root@bogon ~]# Path=${path};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
[Root@bogon ~]# echo ${path#/*:} =>key to *. (* for wildcard characters)
/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/bin:/root/bin
Example: ${var##/key}
Copy Code code as follows:
[Plain] View plaincopyprint?
[Root@bogon ~]# Path=${path};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
[Root@bogon ~]# echo ${path##/*:}
/root/bin
H. Variable content substitution
Grammar:
${var/old string/new string}: Replace the first string that satisfies the condition
${var//old string/new string}: Replace all strings that meet the criteria
Example: ${var/old string/new string}
Copy Code code as follows:
[Plain] View plaincopyprint?
Root@bogon ~]# Path=${path};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
[Root@bogon ~]# Echo ${path/sbin/sbin}
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/ Bin:/root/bin
Example: ${var//old string/new string}
Copy Code code as follows:
[Plain] View plaincopyprint?
[Root@bogon ~]# Path=${path};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
[Root@bogon ~]# Echo ${path//sbin/sbin}
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/ Bin:/root/bin
Two, environment variables
Ordinary variables can be understood as local variables, environment variables can be understood as global variables, the landing of the successful bash shell is a process, in which case to open a new shell is his child process, A child process is a custom variable that cannot get the parent process, but can get the parent process's environment variable
A. Environment variable Export (export)
syntax:export var
B. Environment variable View (env)
Syntax: env
A few of the more important environment variables
Home: Represents the user's master folder
Shell: Which program represents the shell currently in use, and I am using/bin/bash
Histsize: History maximum number of storage bars
The mailbox file that the system reads when the Mail:mail command system is received
Path: Performing a file lookup path
LANG: Language information
RANDOM: Random number variable (0~32767)
Three, the prompt character setting (PS1)
variable ps1= ' [\u@\h \w]\$ ' records the display format of the command prompt [Root@bogon ~]#
Symbolic meaning
\d: can be displayed [day of the week] The date format, such as: "Mon Feb 2"
\h: Full host name.
\h: Only the name of the host name before the first decimal point
\ t: Displays the time, is a 24-hour [HH:MM:SS]
\ t: Displays the time, is a 12-hour [HH:MM:SS]
\a: Display time, 24-hour format [hh:mm
\@: Displays the time, 12-hour [AM/PM] style
\u: The current user's account name, such as [Root],
\v:bash version information, such as Brother Bird's test board This is 3.2.25 (1), only [3.2] display
\w : The full working directory name, the directory name that is written by the root directory. But the home directory will be replaced by ~;
\w: Use the basename function to get the working directory name, so only the last directory name is listed.
\#: The first few commands issued.
\$: Prompt character, if root, prompt character is #, otherwise $
Example:
Copy Code code as follows:
[Root@bogon ~]# ps1= ' [\u@\h\a \w \#]\$ ']
[Root@bogon23:45 ~ 82]#