shell variables and in-depth basis
- Variable Type
Variable be divided into two categories: the environment variable (global variable) and local variables.
Use any of the child shell environment variables can also be called a global variable, you can create them out of the shell and its derivatives. Local variables can only be used in the creation of their shell function or script. Some variables are user-created others are dedicated shell variables. - Environment Variables
Environment variables are used to define the shell of the operating environment, ensure the correct execution of shell commands, shell to determine Username environment variables, command path, terminal type, login and other directory, all the environmental variables are system global variables available in all sub-processes, which includes editor, shell scripts and a variety of applications.
Environment variables can be set on the command line, but these variable values will be lost when the user exits, it is best to .bash_profile file in the user's home directory or the global configuration / etc / profile file or in /etc/profile.d/ definition. The environment variable file into the profile, the value of these variables are initialized every time the user logs on.
Traditionally, all environment variables are capitalized. Former user environment variables used in the process, must be exported with export command
environment variables can be used in the creation of their shell and any child shell or process derived from the shell. They are often referred to as global variables to distinguish local variables. In general, the environment variables should be capitalized. Environment variables are variables have built-in command to export with export.
There are some environment variables such as HOME, PATH, SHELL, UID, USER , etc., before the user logs had been set / bin / login program, he generally defines environment variables and stored in the user's home directory .bash profile file. Specific environment variable information, refer to the following table: bash Environment Variables
Variable name Meaning
full path name used when BASH = / bin / bash call instance of bash
= last parameter on a command
BASH_VERSINFO = ([0]) = "3" when the [1] = "2" [ 2] = "25" [3] = "1" [4] = "release" [5] = "x86_64-redhat-linux-gnu") using 2.0 or later, expands Version Information
BASH_VERSION = '3.2.25 (1) -release ' version number of the current instance of bash
COLORS = / etc / DIR_COLORS
the COLUMNS = variable is set give the edit mode and command selections shell defines broadband editing window 32.
= Pty consoletype
the CVS_RSH = SSH
of the DIRSTACK = () representative of the current contents of the directory stack
EUID = 0 is a valid ID of the current support of the shell starts to be initialized
G_BROKEN_FILENAMES. 1 =
the GROUPS = 0 groups current user belongs
HISTFILE = / root / .bash_history full path history file
HISTFILESIZE = the maximum number of lines 50 history file can contain the
hOME = / root of the current user's home directory
HOSTNAME = root of the current host machine name
HOSTTYPE = the x86_64
the IFS = $ '\ T \ n-' internal field separator, generally are spaces, tabs, and line breaks, for replacing the command table and reads the input loop structure generated by the field division of the word.
INPUTRC = / etc / inputrc readline startup file name, replace the default ~ / .inputrc
JAVA_HOME = / the Application / jdk1.8.0_10
KDEDIR = / usr
KDE_IS_PRELINKED = 1
KDE_NO_IPV6 = 1
LANG = zh_CN.GB18030 Chinese font variable
= LESSOPEN '/ usr / bin / lesspipe.sh% S'
LINES = 36
LOGNAME = the root login user name
the LS_COLORS = XX
MACHRYPE = the x86_64-RedHat Linux-GNU-string contains a description of the system running the bash
MAILCHECK = 60 This parameter defines the shell How often (in seconds inspections by parameter MAILPATH or MAILFILE) specified files to see if there are messages arrive. The default value of 600 seconds
MAIL = / var / spool / mail / root message full path
OLDPWD = / root before a current working directory
OPTERR = 1 error messages from getopts builtin mmol If set to 1, in ten seconds.
OPTIND = 1 Next there getopts built parameters of the command processing sequence number
OSTYPE = linux-gnu automatically set to a string, operating system of the string descriptor running bash, the default value is determined by the system
PATH = / opt / maven / bin : / usr / local / mysql / bin: / opt / jdk / bin: / opt / jdk / jre / bin: / usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / opt / tomcat / bin: / opt / tomcat_8081 / bin: / root / bin global pATH path. Command search path. A colon-separated list of directories, shell use it to search command. The default path systematically determined and set by the installation bash administrator
PIPESTATUS = ([0] = " 0" [1] = "0") an array comprising a process most into the foreground pipeline execution of the job exit status .
PPID = 1112 parent process ID
PRELENK_FULL_TIME_INTERVAL = 14
prelinking = Yes
PRELINK_NONRPM_CHECK_INTERVAL. 7 =
PRELINK_OPTS = -mr
PS1 = [\ @ U \ H \ W is] \ $ main lift string, the default value is ¥
PS2 = '>' prompt times symbol, the default value is>
the PS4 = '+' when the tracking is turned on so that the debug prompt string, default +, can be used to track open set -x
PWD = / home current user home
QTDIR = / usr / lib64 / qt -3.3
= QTINC / usr / the lib64 / Qt-3.3 / the include
QTLIB = / usr / the lib64 / Qt-3.3 / lib
qt_prefix = / usr / the lib64 / Qt-3.3
PESIN_HOME = / file application / Resin-3.1.6 export this is provided by artificial environment variables java using
the sHELL = / bin / bash
SHELLOPTS are = braceexpand: Emacs: hashall: histexpand: History: Interactive-Comments: Monitor comprises an open shell option
SHLVL = 1 each start a bash example will be adding it. 1
SSH_ASKPASS = / usr / libexec / OpenSSH / SSH-GNOME-askpass
the TERM = VT100
The number of seconds to wait for a timeout before TMOUT = 3600 exit
UID = 0 for the current user's UID, initialized at shell startup.
USER = root of the current user name, initialized at shell startup - Custom environment variables (global variables)
to set environment variables
if you want to set environment variables, it is necessary when using the export command after assign values to variables or set variables, declare built-in command with the -x option can also perform the same function. (Note: Do not add $ before the variable name in the output variable)
table export commands and options
option value
-
-f
-n
-p
format:
export variable name = value
variable name = value; export variable names
declare -x variable name = value
Note: the above three kinds of set environment variables method
Example:
Export NAME = Oldboy
DECLARE the -X-
customized environment variables java environment configuration example of a production environment:
Export the JAVA_HOME = / opt / JDK
Export the JAVA_HOME $ the PATH = / bin: $ the JAVA_HOME / jre / bin: $ the PATH
Export the CLASSPATH = $ the CLASSPATH:. $ JAVA_HOME / lib: $ JAVA_HOME / jre / lib: $ JAVA_HOME / lib / tools.jar
Export TOMCAT_HOME = / opt / Tomcat
Export the PATH = $ the PATH: $ TOMCAT_HOME / bin
on: / etc / profile at or /etc/profile.d/ - Display and cancel environment variable
l print environment variables through the echo command
$ HOME user when logging into the directory
$ UID of the current user's UID (user ID) is equivalent to id -u
absolute pathname of the current working directory of $ PWD
$ SHELL SHELL current
other omitted Some
examples of
[@ yanwei the root-test1 scripts] # $ the HOME echo
/ the root
[@ yanwei the root-test1 scripts] # echo the UID $
0
[@ yanwei the root-test1 scripts] $ # echo the PWD
/ Server / scripts
[the root @ yanwei- test1 scripts] # echo $ SHELL
/ bin / bash
[root @ yanwei-test1 scripts] # echo $ the USER
root
Tip: You can directly use the above default system environment variables when writing shell scripts
l with env (printenv) or set display default environment variable
[@ yanwei the root-test1 scripts] # the env
the TOMCAT_HOME = / opt / tomcat_8081
XDG_SESSION_ID = 1277
HOSTNAME = yanwei-test1
the TERM = the xterm
the SHELL = / bin / the bash
HISTSIZE=1000
SSH_CLIENT=192.168.0.157 49318 22
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:.tar=01;31:.tgz=01;31:.arc=01;31:.arj=01;31:.taz=01;31:.lha=01;31:.lz4=01;31:.lzh=01;31:.lzma=01;31:.tlz=01;31:.txz=01;31:.tzo=01;31:.t7z=01;31:.zip=01;31:.z=01;31:.Z=01;31:.dz=01;31:.gz=01;31:.lrz=01;31:.lz=01;31:.lzo=01;31:.xz=01;31:.bz2=01;31:.bz=01;31:.tbz=01;31:.tbz2=01;31:.tz=01;31:.deb=01;31:.rpm=01;31:.jar=01;31:.war=01;31:.ear=01;31:.sar=01;31:.rar=01;31:.alz=01;31:.ace=01;31:.zoo=01;31:.cpio=01;31:.7z=01;31:.rz=01;31:.cab=01;31:.jpg=01;35:.jpeg=01;35:.gif=01;35:.bmp=01;35:.pbm=01;35:.pgm=01;35:.ppm=01;35:.tga=01;35:.xbm=01;35:.xpm=01;35:.tif=01;35:.tiff=01;35:.png=01;35:.svg=01;35:.svgz=01;35:.mng=01;35:.pcx=01;35:.mov=01;35:.mpg=01;35:.mpeg=01;35:.m2v=01;35:.mkv=01;35:.webm=01;35:.ogm=01;35:.mp4=01;35:.m4v=01;35:.mp4v=01;35:.vob=01;35:.qt=01;35:.nuv=01;35:.wmv=01;35:.asf=01;35:.rm=01;35:.rmvb=01;35:.flc=01;35:.avi=01;35:.fli=01;35:.flv=01;35:.gl=01;35:.dl=01;35:.xcf=01;35:.xwd=01;35:.yuv=01;35:.cgm=01;35:.emf=01;35:.axv=01;35:.anx=01;35:.ogv=01;35:.ogx=01;35:.aac=01;36:.au=01;36:.flac=01;36:.mid=01;36:.midi=01;36:.mka=01;36:.mp3=01;36:.mpc=01;36:.ogg=01;36:.ra=01;36:.wav=01;36:.axa=01;36:.oga=01;36:.spx=01;36:*.xspf=01;36:
MAVEN_HOME=/opt/maven
MAIL=/var/spool/mail/root
PATH=/opt/maven/bin:/usr/local/mysql/bin:/opt/jdk/bin:/opt/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/tomcat/bin:/opt/tomcat_8081/bin:/root/bin
PWD=/server/scripts
JAVA_HOME=/opt/jdk
LANG=zh_CN.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
CLASSPATH=.:/opt/jdk/lib:/opt/jdk/jre/lib:/opt/jdk/lib/tools.jar
22 is 192.168.0.235 192.168.0.157 = 49318 SSH_CONNECTION
LESSOPEN% S = || /usr/bin/lesspipe.sh
XDG_RUNTIME the DIR = / RUN / User / 0
HISTTIMEFORMAT the root T =% F.%
= / Usr / bin / the env
OLDPWD = / root
tips: 1. use set all local variables
2. we can also customize the global environment,
L cancel local variables and environment variables unset
example:
[root @ test1 yanwei-scripts] # Export AA = LL
[root @ yanwei scripts -test1] # echo $ AA
LL
[root @ test1 yanwei-scripts] # unset AA
[root @ test1 yanwei-scripts] # echo $ AA
# here is a blank line output