Bash variables in Linux Programming

Source: Internet
Author: User
Tags line editor
Linux programming-Bash variables-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. User-Defined variables
A user-defined variable consists of letters, numbers, and underscores. the first character of the variable name cannot be a number.
Like other UNIX names, variable names are case sensitive.
You can assign values to variables as follows:
Name = value
When referencing a variable, you must add the $ symbol before it. You can assign values to each other between variables, for example:
(The previous $ is a command prompt)

$ JOHN = john
$ NAME = $ JOHN
$ Echo Hello $ NAME
Hello john

You can also use variables and other characters to form a new word. In this case, you may need to include the variable with {}, for example:

$ SAT = Satur
$ Echo Today is $ {SAT} day
Today is Saturday

Bash treats unassigned variables with null values. You can use the unset command to clear the variables.
Value.

Array variables can also be used in Bash. There are two assignment types:

(1) name [index] = value
(2) name = (value1... valuen) The subscript starts from 0.

There is no limit on the range of objects under the array, and continuous components are not required.

The built-in commands for variables in Bash include:

(1) declare and typeset have the same functions. The options include:
[-/+] A sets/revokes the array attribute of a variable.
[-/+] I set/undo the integer attribute of a Variable
[-/+] R setting/Revoking the read-only attribute of a Variable
[-/+] X sets/revokes the output attribute of a variable.
-P var: DISPLAY variable attributes
(2) export and local.
Export outputs the variable to the environment. Usage:
Export name
Export name = value

Here we need to briefly introduce the role of export: When Bash shell executes
A new execution environment for the program, called the sub-shell,
In Bash Shell, the variables are local, that is, they are only the sub-
It makes sense in Shell. After export is used, the variable is set as a global variable.
Can be recognized by other sub-shells.

If the local variable is marked as local (for example, it can only be used inside the function), the usage is:
Local name
Local name = value
(3) readonly.
The specified variable is read-only. After execution, the change quantity cannot be assigned again. Usage:
Readonly name

2. Location variables or Shell Parameters
When interpreting USER commands, Bash Shell uses the first child of the command line as the command, while other words
Parameters are passed to the program through location variables. $1,..., $9 respectively represent the first,..., nine parameters. Among them, 1-9
Is the real parameter name, "$" is only used to identify the replacement of variables.
The position variable $0 indicates the executable name corresponding to the command.
Others include:
$ # Number of parameters sent to the command
$ @ All parameters, each enclosed in double brackets
$ * All parameters, enclosed in double brackets

3. Shell-related variables
(1) Some Common variables set by Shell:
Line number of the command being executed by LINENO in the script
The full name of the current directory of the PWD user
The full name of the user's current directory before OLDPWD last executes cd
PPID parent process ID
$ Current process ID
RANDOM Number of RANDOM (range: 0-32767)
SECONDS Bash Shell running time, in SECONDS
REPLY select and read commands.
OPTARG
ORTIND is set by the getopt command.
UID: the User ID of the current User.
_ The last parameter used by the previous command
(2) Some common environment variables that affect Shell behavior:
The PATH command is used to search for the PATH, which is separated by a colon,
The current directory is not in the system path.
The path name of the HOME directory of the home user. It is the default parameter of the cd command.
COLUMNS defines the length of the command line that can be used in the Command editing mode.
EDITOR default Line EDITOR
VISUAL default VISUAL editor
FCEDIT command fc Editor
History file of the HISTFILE command
The maximum number of commands that can be included in the history file of the HISTSIZE command
The maximum number of lines in the history file of the HISTFILESIZE command.
IFS defines the Separator Used by SHELL
LOGNAME
MAIL points to a file whose modification time needs to be monitored by SHELL. After the file is modified,
SHELL sends the message You hava mail to the user
Mailcheck shell checks the cycle of the MAIL file, in seconds
The MAILPATH function is similar to the MAIL function. However, you can use a group of files separated by colons.
It can be followed by a question mark and a message sent to the user.
SHELL path
TERM terminal type
The time when tmout shell exits automatically, in seconds. If it is set to 0, SHELL exits automatically.
PROMPT_COMMAND specifies the command to be executed before the main command prompt
PS1 main command prompt
PS2 second-level command prompt, which must be used for data input during Command Execution
Command Prompt for PS3 select
PS4 debugging command prompt
MANPATH: Find the path of the manual page, separated by a colon
LD_LIBRARY_PATH: Find the library path, separated by a colon
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.