9th. Shell Basics (3) _bash variables

Source: Internet
Author: User

4. Bash 's variables

4.1 user-defined variables

4.1.1 What is a variable

A variable is a memory unit of a computer where the stored value can be changed. When a shell script needs to save some information, such as a file name or a number, it is stored in a variable. Each variable has a name, so it's easy to refer to it. Variables can be used to save useful information, make the system aware of user-related settings, variables can also be used to save temporary information.

4.1.2 variable Setting rules

(1) Variable names can consist of letters, numbers, and underscores, but cannot begin with a number. If the variable name is "2name" it is wrong.

(2) in bash, the default type of a variable is a string type, and if you want to perform a numeric operation, the mandatory variable type is numeric. ( e.g. aa=11,bb=22,cc= $aa + $BB, CC is 11+22, not.)

(3) The variable is connected with an equal value, and the left and right sides cannot have spaces

(4) If the value of the variable has a space , you need to include either single or double quotation marks .

(5) In the value of the variable, you can use the "\" escape character

(6) If you need to increase the value of the variable, then the value of the variable can be superimposed. However, the variable needs to contain "$ variable name" in double quotation marks or with ${variable name} .

(7) If you assign the result of the command as a variable value to the variable, you need to use the back quotation mark or the $ () containing command . such as #echo $ (date)

(8) The environment variable name is recommended capitalization, easy to distinguish.

4.1.3 Variable Classification

(1) User-defined variables

(2) Environment variables: This variable is mainly stored in the operating system environment-related data.

(3) Positional parameter variable: This variable is mainly used to pass parameters or data to the script , the variable name cannot be customized, and the variable function is fixed.

(4) Predefined variables: It is a variable that has been defined in bash, the variable name cannot be customized, and the variable function is fixed.

4.1.4 Local Variables

(1) variable definition: #name = "Santa Claus"

(2) Variable overlay

#aa =123

#aa = "$aa" 456//aa=123456

#aa =${aa}789//aa=123456789

(3) Call to variable: #echo $AA

(4) Variable view: #set

(5) Variable deletion: #unset variable name

4.2 Environment Variables

(1) What is the environment variable?

The user-defined variable takes effect only in the current Shell   , and the environment variable takes effect in the current shell and all of the shell's child shells . If the environment variable is written to the appropriate configuration file, the environment variable will take effect in all shells.

(2) Setting environment variables

① declaration variable: #export variable name = variable value, or export variable name. such as #export age=18

② query variable: #env

 ③ Delete variable: #unset variable name

(3) Environment variables common to the system

① the path to the System Lookup command:$PATH. (Path variable overlay: #PATH = "$PATH":/root/sh, Path to: delimited)

  ②PS1: Defining a variable at the system prompt

Parameters

Description

\d

The date is displayed in the format "Day of the Week"

\h

Displays the abbreviated host name. such as the default hostname "localhost"

\ t

Displays 24-hour time in the format "HH:MM:SS"

\ t

Displays 12-hour time in the format "HH:MM:SS"

\a

Displays 24-hour time in the format "hh:mm"

\u

Show Current user name

\w

Displays the full name of the current directory

\w

Displays the last directory in the current directory

\#

The first few commands to execute

\$

Prompt. If the root user displays the prompt as "#", if it is a normal user, the prompt will be displayed as "$"

4.3 Positional parameter Variables

Positional parameter variables

Role

$n

N is a number, and $ Arepresents the command itself ,$1-$9 represents the 1th to 9th arguments , and more than 10 of the arguments need to be enclosed in curly braces, such as ${10}

$*

This variable represents all the arguments in the command line ,$* all the parameters as a whole

[Email protected]

This variable also represents all parameters in the command line, but [email protected] treats each parameter differently

$#

This variable represents the number of arguments in the command line.

"Programming Experiment" positional parameter variables

#!/bin/BashEcho "A total of $# parameters"#参数个数Echo "The parameters is: $*"#显示所有参数Echo "The parameters is: [email protected]"#所有参数 forIinch "$*" Do    Echo "The paramters is: $i" Donex=1 forIinch "[email protected]" Do    Echo "The parameter $x is: $i"x=$ (($x +1)) DoneNUM1=$1num2=$2sum=$ (($num 1 +$num 2)) #两个数相加Echo "sum = $sum"

4.4 pre-defined variables

(1) Pre-defined variables

Pre-defined variables

Role

$?

The return status of the last command executed . If the value of this variable is 0, the previous command is executed correctly, and if not 0, the previous command was executed incorrectly. Available #echo $? test

$$

Process PID of the current process

$!

Process PID of the last process running in the background

"Programming Experiment" shows the current process and the process running in the background

[[Email protected]SH]#ls9.5.SHHello.SHparam1.SH[[Email protected]SH]#chmod 755 9.5.SH[[Email protected]SH]#Cat 9.5.SH#!/bin/BashEcho "The current process is $$"#使用find命令在root目录下查找hello. sh File # symbol&it means to put the find command in the background execution. Find/root-name Hello.SH&Echo "The last one Daemon process is $!"[[Email protected]SH]# ./9.5.SHThe current process is1502 the LastOne Daemon process is1503

(2) Receive keyboard input #read [options] [variable name]

options

Description

-P " message"

While waiting for the read input, output prompt

-T number of seconds

The read command waits for user input, using this option to specify the wait time

-N characters

Read command accepts only the specified number of characters, executes

-S

Hide input data , input for confidential information

"Programming Experiment" receives keyboard input

#!/bin/Bashread-T --P"Please input your name:"name# Tips"Please enter your name"and wait 30 seconds to save the user input in the variable nameEcho "Name is $name"Read-s-t --P"Please enter your age:"age# age is privacy, so use"- S"option to hide inputEcho-E"\ n"Echo "Age is $age"Read-N1-T --P"Please select your gender[m/f]:"gender# Use"- N 1"option to receive only one input character will be executed (without entering the carriage)Echo-E"\ n"Echo "Sex is $gender"

9th. Shell Basics (3) _bash variables

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.