Learning internal variables

Source: Internet
Author: User

Internal Variable learning built-in variables, these variables will affect the behavior of the bash script. $ BASH Bash binary program file path root @ ubuntu :~ /Resource/shell-study/0506-2013 # echo $ BASH/bin/bash $ BASH_ENV environment variable points to a Bash Startup File. When a script is called, the Startup file will be read. $ BASH_SUBSHELL this variable is used to prompt the sub-shell hierarchy. this is a new Bash feature. It was not introduced until Version 3 Bash. $ BASH_VERSINFO [n] This is an array containing six elements, which contains the installed Bash version information. this is very similar to the $ BASH_VERSION below, but it is more detailed. root @ ubuntu :~ /Resource/shell-study/0506-2013 # for n in 0 1 2 3 4 5> do> echo "BASH_VERSINFO [$ n] =$ {BASH_VERSINFO [$ n]}"> done BASH_VERSINFO [0] = 4 BASH_VERSINFO [1] = 1 BASH_VERSINFO [2] = 5 BASH_VERSINFO [3] = 1 BASH_VERSINFO [4] = release BASH_VERSINFO [5] = i486-pc-linux-gnu $ BASH_VERSION bash version installed on the system root @ ubuntu: ~ /Resource/shell-study/0506-2013 # echo $ BASH_VERSION 4.1.5 (1) -release: the output information exactly corresponds to the information found in the preceding array. $ DIRSTACK is the top value in the directory stack. this built-in variable is consistent with the dirs command, but the dirs command displays the entire contents of the Directory stack. $ the default EDITOR called by the EDITOR script, usually vi or emacs. $ EUID "valid" User ID. No matter what user is assumed as the current user, this number is used to indicate the ID number of the current user, or the su command may be used to achieve this assumption ., $ EUID is not necessarily the same as $ UID. $ FUNCNAME (name of the current function )#! /Bin/bash func () {echo "$ FUNCNAME is running."} func echo "FUNCNAME = $ FUNCNAME" exit 0 result: root @ ubuntu :~ /Resource/shell-study/0506-2013 #./test7.sh func is running. FUNCNAME = root @ ubuntu :~ /Resource/shell-study/0506-2013 # the second row has no name, because FUNCNAME can only be used inside the function method to use the $ GLOBIGNORE pattern match list for a file name) the matched file package in contains a file in this list, the file will be removed from the matching result. $ GROUPS: the group to which the current user belongs. This is a list of group IDs of the current user (array), which is the same as the content recorded in the/etc/passwd file. $ HOME indicates the home Directory of the user, generally,/home/usernam $ HOSTNAMEhostname is put in an initialization script and a system name is allocated when the system is started. however, the gethostname () function can be used to set the Bash internal variable $ HOSTNAME $ HOSTTYPE host type, just like $ MACHTYPE, to identify system hardware. root @ ubuntu :~ /Resource/shell-study/0506-2013 # echo $ HOSTNAME ubuntu root @ ubuntu :~ /Resource/shell-study/0506-2013 # echo $ HOSTTYPE imo-root @ ubuntu :~ /Resource/shell-study/0506-2013 # echo $ HOME/root @ ubuntu :~ /Resource/shell-study/0506-2013 # echo $ GROUPS 0 root @ ubuntu :~ /Resource/shell-study/0506-2013 # $ IFS internal domain separator. This variable is used to determine how Bash recognizes the domain or word boundary when interpreting strings. $ IFS is blank (space, tab, and line break) by default, but this can be modified. For example, you can set it to a comma when analyzing data files separated by commas. note that $ * uses the first character saved in $ IFS #! /Bin/bash output_args_one_per_line () {for arg do echo "[$ arg]" done} echo "IFS = \" \ "IFS =" "var =" a B c "output_args_one_per_line $ var echo "----------------" echo "IFS =: "IFS =": "var =": a: B: c: "output_args_one_per_line $ var exit 0 result: root @ ubuntu :~ /Resource/shell-study/0506-2013 #. /test8.sh IFS = "" [a] [B] [c] ---------------- IFS =: [] [a] [] [B] [c] [] [] root @ ubuntu: ~ /Resource/shell-study/0506-2013 # It can be seen that the results are still somewhat different when IFS = "" and ":" are used, the same thing will happen in the "FS" domain of the awk. $ IGNOREEOF ignore EOF: tells shell how many file Terminators (control-D) should be ignored before log out ). $ LC_COLLATE is always in. bashrc or/etc/profile. This variable is used to control the order of extension of file names and pattern matching. if $ LC_COLLATE is incorrectly set, LC_COLLATE will generate unexpected results in filename globbing. $ LC_CTYPE this internal variable is used to control the string interpretation in globbing and pattern matching. $ LINENO: this variable is used to record the row number in the script. this variable makes sense only when the script uses this variable, and is generally used for debugging purposes. $ MAC HTYPE indicates the hardware of the system. $ working directory before OLDPWD ("OLD-print-working-directory", which is your previous directory) $ OSTYPE operating system type $ PATH: Search PATH of executable files, usually/usr/bin/,/usr/X11R6/bin/,/usr/local/bin, and so on. $ PPID is the ID (pid) of the parent process of the process ). [1] $ PROMPT_COMMAND this variable saves the command to be executed before the main prompt $ PS1 is displayed. $ PS1 this is the main prompt, which can be seen in the command line. $ PS2 second prompt, when you need additional input, you will see it. "> ". the third prompt of $ PS3 is displayed in a select loop (see example 10-29 ). $ PS4 fourth prompt: when you use the-x option to call the script, this prompt will appear at the beginning of each line of output. "+" is displayed by default ". $ PWD working directory (your current directory), which works the same as the built-in command pwd. $ REPLY when no parameter variable is provided to the read command, this variable will be provided to the read command as the default variable. it can also be used in the select menu, but only the number of the selected variable is provided, not the value of the variable itself #! /Bin/bash # REPLY is the default variable provided to the 'read' command. echo-n "What is your favorite vegetable? "Read echo" Your favorite vegetable is $ REPLY. "# REPLY stores the value read by the last" read "command only when no variable is provided to the" read "command. echo-n "What is your favorite fruit? "Read fruit echo" Your favorite fruit is $ fruit. "echo"... value of \ $ REPLY is still $ REPLY. "# $ REPLY still saves the value of the previous read command. # + the variable $ fruit is passed into this new" read "command. exit 0 result: root @ ubuntu :~ /Resource/shell-study/0506-2013 #./test9.sh What is your favorite vegetable? Your favorite vegetable is. What is your favorite fruit? Apple Your favorite fruit is apple. but... Value of $ REPLY is still. root @ ubuntu :~ /Resource/shell-study/0506-2013 # $ SECONDS the time when the script has been run (in SECONDS ).#! /Bin/bash TIME_LIMIT = 10 INTERVAL = 1 echo "Hit Control-C to exit before $ TIME_LIMIT seconds. "while [" $ SECONDS "-le" $ TIME_LIMIT "] do if [" $ SECONDS "-eq 1]; then units = second else units = seconds fi echo "This script has been running $ SECONDS $ units. "# In a slow or overloaded machine, the script may ignore the count in a single loop. sleep $ INTERVAL done echo-e "\ a" # Beep! (Beep !) Exit 0 result: root @ ubuntu :~ /Resource/shell-study/0506-2013 #. /test10.sh Hit Control-C to exit before 10 seconds. this script has been running 0 seconds. this script has been running 1 second. this script has been running 2 seconds. this script has been running 3 seconds. this script has been running 4 seconds. this script has been running 5 seconds. this script has been running 6 seconds. this script has been running 7 secon Ds. This script has been running 8 seconds. This script has been running 9 seconds. This script has been running 10 seconds. root @ ubuntu :~ /Resource/shell-study/0506-2013 # $ list of activated options in SHELLOPTSshell, which is a read-only variable. bash $ echo $ SHELLOPTSbraceexpand: hashall: histexpand: monitor: history: interactive-comments: emacs $ SHLVLShell level, which is the depth of Bash nesting. $ SHLVL is 1 in the command line, and $ SHLVL is 2 in the script. $ TMOUT if the $ TMOUT environment variable is set to a non-zero value of time, the shell prompt will time out after time seconds. this will cause logout ). #! /Bin/bash # TMOUT = 3 TIMELIMIT = 5 PrintAnswer () {if ["$ answer" = TIMEOUT]; then echo $ answer else echo "Your favorite veggie is $ answer" kill $! Fi} TimerOn () {sleep $ TIMELIMIT & kill-s 14 $ &} Int14Vector () {answer = "TIMEOUT" PrintAnswer exit 14} trap Int14Vector 14 echo "What is your favorite vegetable" TimerOn read answer PrintAnswer exit 0 result: root @ ubuntu :~ /Resource/shell-study/0506-2013 #./test11.sh What is your favorite vegetable TIMEOUT root @ ubuntu :~ /Resource/shell-study/0506-2013 #./test11.sh What is your favorite vegetable all Your favorite veggie is all root @ ubuntu :~ /Resource/shell-study/0506-2013 # ^ C $ UID user ID, which is recorded in the/etc/passwd file, this is the real id of the current user. This id will not be changed even if it is temporarily changed to another user id by using the su command. $ UID is a read-only variable. It cannot be modified in the command line or script, and it is similar to the built-in id command. #! /Bin/bash ROOT_UID = 0 if ["$ UID"-eq "$ ROOT_UID"]; then echo "You are root. "else echo" You are just an ordinary user. "fi ROOTUSER_NAME = root username = 'whoam' # username = 'id-nu 'if [" $ username "=" $ ROOTUSER_NAME "]; then echo" You are root. "else echo" You are just an ordinary user. "fi exit 0 result: root @ ubuntu :~ /Resource/shell-study/0507-2013 # chmod + x test1.sh root @ ubuntu :~ /Resource/shell-study/0507-2013 #. /test1.sh You are root. you are root. location parameters $0, $1, $2, and so on are passed from the command line to the script or to the function, or set to variable $ # Number of command line parameters or location parameters $ * All location parameters are regarded as a word, and "$ *" must be referenced. $ @ is the same as $ *, but each parameter is an independent reference string. This means that the parameter is completely passed and is not interpreted or extended. this also means that each parameter in the parameter list is regarded as a separate word. Of course, "$ @" should be referenced. #! /Bin/bash E_BADARGS = 65 if [! -N "$1"]; then echo "usage: 'basename $ 0' args1 args2 etc. "exit $ E_BADARGS fi index = 1 echo" Listing args with \ "\ $ * \" "for arg in" $ * "do echo" arg # $ index = $ arg "let "index + = 1" done echo "entire arg list seen as single word. "index = 1 echo" Listing args with \ "\$ @ \" "for arg in" $ @ "do echo" arg # $ index = $ arg "let" index + = 1 "done echo" arg list seen as separate words. "index = 1 echo" Listing args With \ $ * (unquoted ): "for arg in $ * do echo" arg # $ index = $ arg "let" index + = 1 "done echo" Arg list seen as separate words. "exit 0 result: root @ ubuntu :~ /Resource/shell-study/0507-2013 #. /test2.sh 1 2 34 Listing args with "$ *" arg #1 = 1 2 34 entire arg list seen as single word. listing args with "$ @" arg #1 = 1 arg #2 = 2 arg #3 = 34 arg list seen as separate words. listing args with $ * (unquoted): arg #1 = 1 arg #2 = 2 arg #3 = 34 Arg list seen as separate words. root @ ubuntu :~ /Resource/shell-study/0507-2013 # shift usage instructions :#! /Bin/bash echo "$ @" shift echo "$ @" shift echo "$ @" result; root @ ubuntu :~ /Resource/shell-study/0507-2013 #./test3.sh 1 2 3 4 5 6 1 2 3 4 6 6 2 3 4 6 3 4 5 6 root @ ubuntu :~ /Resource/shell-study/0507-2013 # $-mark passed to the script (use the set command ). see example 11-15. this was originally a ksh structure and was introduced to Bash later, but unfortunately it seems that it cannot be used reliably in Bash scripts. one possible use is to allow a script to test whether it can interact with each other. $! PID (process ID) $ _ Of the last job running in the background saves the value of the last parameter of the previously executed command .#! /Bin/bash echo $ _ #/bin/bash du>/dev/null # No output is displayed on the command line. echo $ _ # du ls-al>/dev/null # No output is made on the command line. echo $ _ #-al (this is the final parameter): echo $ _ result: root @ ubuntu :~ /Resource/shell-study/0507-2013 #./test4.sh./test4.sh du-al: root @ ubuntu :~ /Resource/shell-study/0507-2013 # $? Command, function, or exit status code of the script itself #! /Bin/bash echo $? Cat file # wrong command echo $? Echo "right command." echo $? Exit 0 result: root @ ubuntu :~ /Resource/shell-study/0507-2013 #./test5.sh 0 cat: file: No such file or directory 1 right command. 0 root @ ubuntu :~ /Resource/shell-study/0507-2013 # $ ID of the script's own process. $ variables are often used in scripts to construct "unique" temporary file names

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.