The variables in bash

Source: Internet
Author: User
Tags alphanumeric characters line editor

The variables in bash
1. User-defined variables
definition of a variable
User-defined variables consist of alphanumeric characters and underscores, and the first character of a variable name cannot be a number.
As with other UNIX names, variable names are case-sensitive.
For variables, the user can assign values as follows:
Name = value
a reference to a variable
When referring to a variable, you need to precede the $ sign, and the user can assign a value to each other (the value of one variable is assigned to another variable), such as:
(The preceding $ is a command prompt)
$JOHN =john
$NAME = $JOHN
$echo Hello $NAME
Hello John

  You can also make new words with variables and other characters,you may need to use the variable {}Enclosed, such as:

$ SAT = Satur
$ echo Today is ${sat}day
Today is Saturday

For unassigned variables, bash is treated with a null value, and the user can also clear the variable with the unset command
The assigned value.

You can also use array variables in bash, which are assigned two kinds of values:

(1) Name[index] = value
(2) name = (value1 ... valuen) The subscript starts at 0
Ch= (1 2 3 4)
echo "${ch[0]}"
N=1
echo "${ch[$n]}"

There is no limit to the range of array subscripts, and there is no need to use contiguous components.

Bashthe built-in commands about variables are:

(1) Declare and typeset. The two have the same function. The options are:
[-/+]a set/Undo variable's Array property
[-/+]i set/Undo variable's Integer property
[-/+]r set/Undo variable's read-only property
[-/+]x setting/Undo variable's Output property
-p VAR display variable properties
(2) Export and Local.
Export exports the variables to the environment using the following:
Export name
Export NAME = value

Here's a brief introduction to the role of export: when the bash Shell executes a
program, a new execution environment, called a child shell, is first created for the program,
In bash shell, variables are local, that is, they are just creating their child
It is meaningful in the shell that, with export, the variable is set to a global variable, which
Can be identified by other child shells

Local tag variables are localized (such as can only be used inside the function), using:
Local name
Local name = value
(3) readonly.
The specified variable is read-only, and after execution, the amount of change cannot be re-assigned, using:
ReadOnly Name
2. Positional variables or shell parameters
When the Bash Shell interprets a user command, it takes the first word of the command line as a command,
Other words are passed to the program as arguments through positional variables. $,..., $9 represents the first,..., nine parameters respectively.
Where 1-9 is the true parameter name, the "$" character is used only to identify the substitution of the variable.
Position variable----refers to the executable name of the command.
The others are:
$# the number of arguments sent to the command
[email protected] All parameters, each enclosed in double brackets
$* all parameters, enclosed in double brackets
3. Shell-related variables
(1) Some common variables that the shell itself sets:
Lineno the line number of the command being executed in the script
PWD user The full name of the current directory
Oldpwd the full name of the user's current directory before the last time the CD was executed
PPID Parent Process ID
$ Current Process ID
Random number (range 0-32767)
SECONDS Bash Shell run time, unit is seconds
REPLY Select and read commands are used and will be discussed later
Optarg
Ortind These two variables are set by the getopt command
UID Current User ID
_ Last parameter used by the previous command
(2) Some common environment variables that affect shell behavior:
The path command searches for paths with colons as delimiters. Note Unlike DOS,
The current directory is not in the system path
The path name of the home user's home directory, which is the default parameter for the CD command
COLUMNS defines the length of the command line that can be used in command-editing mode
Editor's default line editor
Visual Editor by default
Fcedit Command FC using the editor
Histfile Command History file
The maximum number of command bars that can be included in a histsize command history file
Maximum number of rows contained in the histfilesize command history file
IFS defines the delimiter used by the shell
LOGNAME User Login Name
MAIL points to a file that requires the Shell to monitor its modification time. When the file is modified,
Shell will send messages to you HAVA mail to users
MailCheck Shell checks the period of the mail file in seconds
The Mailpath function is similar to mail. But you can use a set of files, separated by colons, after each file
Can follow a question mark and a message sent to the user
The path name of the shell shell
Term terminal type
Tmout Shell Automatic exit time, in seconds, if set to 0 prohibit shell auto-exit
Prompt_command Specifies the command to be executed at the main command prompt
PS1 main command Prompt
PS2 level two command prompt, which is required to enter data during command execution
Command Prompt for PS3 select
PS4 Debug Command Prompt
MANPATH find the path to the manual page, separated by a colon
Ld_library_path find the path to the library, separated by a colon
Action on variables in bash
1.Conditional variable Substitution:
The Bash shell can make conditional substitutions of variables, replacing only when certain conditions occur, replacing
The condition is placed in {}.
(1) ${value:-word}
When the variable is undefined or the value is empty, the return value is the contents of Word, otherwise the value of the variable is returned.
(2) ${value:=word}
Similar to the former, except that if the variable is undefined or the value is empty, the value returned in Word will be
Word assigns values to value
(3) ${value:?message}
If the variable is assigned a value, replace it normally. Otherwise, send message messages to the standard error output (if
This substitution appears in the shell program, and the program will terminate running)

The variables in bash

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.