Shell Learning Note II (variable)

Source: Internet
Author: User

    is a weakly typed variable that does not need to specify its variable type when declaring it, and does not need to follow the "declare before use" and can be used whenever you want. A variable in which a local variable is in effect in a shell is not valid in another shell. You can use the local built-in command to "display" the declared local variables in the environment variable shell script, which defaults to the global preset environment variable: bash: Description of Bash's full path bash_version Cdpath: For quick access to the directory Euid: Log The current user's UID FUNCNAME: Inside the user function body, record the function name of the current function body Histcmd: Record the number of the next command in the history command histfile: Record h Location of the istory command's record file Histfilesize HOSTNAME HOSTTYPE: Host architecture MACHTYPE: GNU flag for Host type LANG: Language environment PWD : Record current directory oldpwd: Log before directory PATH PS1: command prompt, default is [\[email protected]\h \w]\$, where \u is the user name, \h is the hostname, \w is the current    The basename,\$ of the working directory is the replacement character for the user uid. Define and use variables Var=value Note: 1, = no space on either side var = value (X) 2, the variable has a space when it must be "" or "' cause to take value: ${var} or $var Note: 1. If the variable value refers to a different variable, you must use double quotation marks.        Because single quotes prevent the shell from interpreting special characters $.        2. Because the shell is a weak variable, it can be referenced even if the variable is not defined in the script, making the error difficult to find. 3, the shell has the characteristics of weak variables, do not need to declare can be used, you can set the variable must first declare and then use Shopt-s-o nounset Cancel the variable unset var cancellation function unset fun Ction Special variable positional parameter $ (n) $ n>10 $n When you are in the,...,            Some parameters $# script parameter sum script or command return value $? The return value of the previous command to see the return value of a command must be viewed immediately after running a command. The normal exit commands and scripts in Linux should be 0 as their return value, and any return value other than 0 indicates that the command did not exit correctly or execute properly in an Automation script, or you can use the value of the $? variable to determine the execution state of the previous command, thus taking a different array of actions Shel            L variables are weakly typed, and not every element of the array is of the same type, the array in the shell has no limit on the number of elements, but only one-dimensional arrays are supported. The Shell's declaration of the array variable is very loose, and you can add the element definition in the variable as needed at any time.                Declare-a Array initialization array[0]=0 array[1]=1 array[2]= "Hello" or              Declare-a array= (0 1 "hello") array[3]= ' WaNG ' or array= (' John ' Sue ') or array= ([3]=3] [4]=4 [7]=4] Jump number Assignment array operation value ${ Arrayname[index]} Example: 1, echo ${array[0]}//Take a single element 2, echo ${array[@]}//                 Take all the element values and get the element values separated by a space   3, Echo ${array[*]}//Take all the element values, get is a whole string 4, ARR1=ARR2//Such equivalent to arr1=arr2[0] length Use @ or * to expand an array into a list and then use # to get an example of an array element: 1, echo ${#array [@]} 2, Echo ${#                Array[*]} Through this example also know to find the length of the string, ${#string} intercept a part of an element, the object can be an entire array or an element Example: 1, take out the first of the array, the second element Echo ${array[@]:1:2} 2, take out the second element from the No. 0 Starting with 5 consecutive characters echo ${array[2]:0:5} connection conn= (${arr1} ${ARR2}) Replace array element    Array= (${array[@]/old/new}) cancels array or element unset Array[0] unset array The read-only variable readonly ro=100 is equivalent to: the scope of the DECLARE-R ro=100 variable 1, the scope of the shell variable is within the shell, the global variable belonging to this shell, that is, from the definition of the variable        To the end of the shell, or to actively use unset to delete the variable where it is. 2. After a local variable with the same name as the global variable is declared within a function question, the operation on the variable will only affect the local variable, without affecting the global variable with the same name.

Shell Learning Note II (variable)

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.