Initial knowledge of Shell environment variables (sixth chapter)

Source: Internet
Author: User
Tags define local types of functions

1. The concept of environmental variables
Environment variables are used to store shell sessions and work environments, allowing the shell to easily access the program in the set value, environment variables are divided into global variables and local variables

2. Global variables
Global variables take effect in both the shell and its child shells, and you can view the current environment variables through the ENV and PRINTENV commands
[[Email protected] ~]# env
Hostname=localhost.localdomain
Selinux_role_requested=
Term=linux
Shell=/bin/bash
histsize=1000
ssh_client=192.168.186.1 63544 22
Selinux_use_current_range=
qtdir=/usr/lib64/qt-3.3
Qtinc=/usr/lib64/qt-3.3/include
Ssh_tty=/dev/pts/1

[[email protected] ~]# Print env
-bash:print:command not found
[Email protected] ~]# printenv
Hostname=localhost.localdomain
Selinux_role_requested=
Term=linux
Shell=/bin/bash
histsize=1000
ssh_client=192.168.186.1 63544 22
Selinux_use_current_range=
qtdir=/usr/lib64/qt-3.3
Qtinc=/usr/lib64/qt-3.3/include
Ssh_tty=/dev/pts/1
User=root

3. Local Variables
Local variables are only valid for the current shell, no specific commands can be viewed, and all defined variables can be displayed through the SET command
Variable assignment:
Variable name = ' Variable value '
Variable Value:
Direct use of strings; Name= ' username '
variable reference; name= ' $username '
command reference; name= ' Command ', name=$
Variable reference: $ (name), $name
"": a weak reference in which the variable reference is replaced with a variable value
': Strong reference, where the variable reference is not replaced with a variable value and preserves the original string
Variable destruction:
unset name

4. Other types of variables
Local variables: Valid only for the current shell process, not for shells and zishell outside the current shell.
Position variable: $1;$2 ... to indicate that the script is used to invoke arguments in the script code that pass it through the command line;
$1;$2 ...: corresponding to call 1th, 2 .... parameters
Shift [N]
$: Command itself
$*: Pass all parameters to the script, pass all the parameters to the script
[Email protected]: Pass script to all parameters, pass all parameters as an adult to the script
$#: The number of arguments passed to the script.

5. Setting Global Environment variables
Variable declaration, assignment:
Export Name=value
Declare-x Name=value
Variable references:
$name; $ (name)
Show all environment variables:
Export;env;printenv
Destroyed:
unset name
Modifying the Global environment variable of the current shell can take effect on the current shell and its child shell, but has no effect on its parent shell.
There are many built-in environment variables in bash: PATH; SHELL; UID; Histsize; HOME; Pwd;old; Histfile; PS1
Read-only variables:
ReadOnly Name
Declare-r Name
Variable command rule:
Cannot use reserved words in the program, such as If,for;
Use only numbers, letters, and underscores, and cannot start with a number
See the name of righteousness

6. Set the PATH environment variable
Using the Echo $PATH command to view the current PATH environment variable, all parameters of the path variable are separated by (:), and the path variable can be modified using the path= $PATH: NewPath.
The configuration file needs to be modified for long term effect
Bash profiles are divided into the following two categories, according to their effective scope:
Global configuration:
/etc/profile/etc/profile.d/*.sh/etc/bashrc
Personal configuration:
~/.bash_profile ~/.BASHRC

There are two types of functions:
Profile class: Provides configuration for the interactive logon shell
Global:/etc/profile/etc/profile.d/*.sh
Personal: ~/bash_profile
Function: Define environment variables, run commands or scripts
BASHRC class: Providing configuration for non-interactive shells
Global:/ETC/BASHRC
Personal: ~/BASHRC
Function: Define variable aliases, define local variables
Configuration file reading order:
Interactive logon: Use the user name password to log in at the terminal or use Su-username and su-l USERNAME to switch users under the current user.
/etc/profile--/etc/profile.d/*.sh--and ~/bash_profile--~/.BASHRC--/ETC/BASHRC
Non-interactive logon: Use SU username to switch between a user or a terminal that is open under a graphical interface
~/.BASHRC--/ETC/BASHRC-/etc/profile.d/*.sh
7. Array variables
Arrays can store multiple values, can be called as a whole, or can be used separately, for example:
Create an array:
[[email protected] ~]# mytest= (one two three four five six) # # # #数组中存储了6个值, corresponding position number 0-5
Displays the second value in the array:
[[email protected] ~]# echo ${mytest[2]}
Three
Displays all values in the array:
[[email protected] ~]# echo ${mytest[*]}
One three four five six
Destroys the 0-bit corresponding value in the array:
[Email protected] ~]# unset mytest[0]
[[email protected] ~]# echo ${mytest[*]}
Three four five six
After destroying 0 of the corresponding value one, the position is still occupied:
[[email protected] ~]# echo ${mytest[0]}

[Email protected] ~]#
Destroying an array:
[Email protected] ~]# unset mytest
[[email protected] ~]# echo ${mytest[*]}

[Email protected] ~]#

This article from "The Boundless" blog, declined reprint!

Initial knowledge of Shell environment variables (sixth chapter)

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.