Shell introduction-bash programming variables

Source: Internet
Author: User
Tags echo name
Article title: shell introduction to bash programming variables. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The variable format of the Bourne Shell is only a string variable. therefore, to use numeric operations, you must rely on external commands to achieve the goal.
. It has the following types of variables.
A: User variable: this is the most commonly used variable. we can use any string that does not contain blank characters as the variable domain name. Set
When setting the variable value, use the following method:
Var = string
When you call a variable, add a "$" number before the variable domain name.
For example, the following test1.sh program:
#! /Bin/bash
Name = tom
Echo name
Echo $ name
The running result is as follows:
Name
Tom
B: System variables (environment variables): similar to user variables, but these variables pass their values to the commands they execute.
To set half a user variable to a system variable, add:
Export var
The following are the system variables that have been set after the user enters the system:
$ HOME user's own directory
$ PATH: the directory to be searched during command execution
$ TZ time zone
$ MAILCHECK the number of seconds to check whether a new letter exists
$ PS1 prompt number in command line
$ PS2 when the command has not been completed, Shell requires entering the prompt number
$ MANPATH man command search path
C: Read-only user variables: similar to user variables, but these variables cannot be changed. Set user variables to read-only.
For example:
Readonly var
If only readonly is used, all read-only variables are listed. Note that the system variables cannot be set to read-only. For example:
#! /Bin/bash
Name = tom
Readonly name
Echo $ name
Name = john
Readonly
The running result is as follows:
Tom
./Test2.sh: name: readonly variable
Declare-ar BASH_VERSINFO = '([0] = "2" [1] = "03" [2] = "0" [3] = "2" [4] =" RELEASE"
[5] = "i691-pc-linux-gnu ")'
Declare-ir EUID = "501"
Declare-ar GROUPS = '()'
Declare-ir PPID = "1190"
Declare-r SHELLOPTS = "braceexpand: hashall: interactive-comments"
Declare-ir UID = "501"
Declare-r name = "tom"
D: Special variable: some variables are set at the beginning of script execution and are not modified, but we do not call them read-only.
System variables, called special variables (some books call them read-only system variables), because these variables are
With this, you cannot set general system variables to read-only. The following are some special variables:
$0 execution name of the program
$ N the nth parameter value of this program, n = 1... 9
$ * All parameters of this program
$ # Number of parameters of this program
$ PID of the program
$! PID used to execute the previous background command
$? Returned value of the previous command
When the number of parameters for executing this program exceeds 9, we can use the shift command to move the parameter forward to one cell.
Use 10th and later parameters. In addition, you can use the set commands $ n and $ * as follows:
Set string
The value of $ * is string, and $ n is placed after decomposition. If there are no parameters after the set command, all
Fixed variables and their values:
File name: ex1 parameter: this is a test
For example:
#! /Bin/bash
Echo Filename: $0
Echo Arguments: $ *
Echo No. of args.: $ #
Echo 2nd arg.: $2
Shift
Echo No. of args.: $ #
Echo 2nd arg.: $2
Set hello, everyone
Echo Arguments: $ *
Echo 2nd arg.: $2
The running result is as follows:
Filename: x1
Arguments: his is a test
No. of args.: 4
2nd arg.: is
No. of args.: 3
2nd arg.:
Arguments: hello, everyone
2nd arg.: everyone
It is worth mentioning that when you want to input a variable value from the keyboard, you can use the following command:
Read var1 var2 ......
In this case, read will distribute a word to a variable. If more words are input than variables, the last variable treats the remaining words
Its value. If less words are entered than the variable, the variable following the variable is set to a null string. If you want to handle numeric operations, I
You can use the expr command.
  
  
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.