linux bash variable scope

linux bash variable scope

First, think about a problem, when executing a program in the shell, shell is how to find this program?

shell will go to $ PATH environment variable defined directory to find the command. Environmental variables typically include /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binso many directories, these directories there are thousands of programs from the directory of so many, so many years to find the program certainly takes time. shell in order to improve search efficiency, use a caching mechanism, this mechanism is called hash. .

With the hash cache, shell hash go inside to find, if found, would use; if not found, before going to the $ PATH environment variable defined directory to find, after finding, plus the Road King commands into the program name the hash.

Note: If the shell command buildin, not into the hash.

  • hash command usage:

    • View all cache hash in which programs are:hash

      hits: representatives of the command, was hit several times.

      # hash
      hits    command
         1    /usr/bin/cat
         2    /usr/bin/ls
    • Detailed view cache hash in which programs are:hash -l

      # hash -l
      builtin hash -p /usr/bin/cat cat
      builtin hash -p /usr/bin/ls ls
    • Clear a cache:hash -d cat

      # hash -d cat
      # hash
      hits    command
         2    /usr/bin/ls
    • Clear all cache:hash -r

      # hash -r
      # hash
      hash: hash table empty
    • Why should clear the cache? Reason: When the command to move to another directory, use the original cache can not find the command, shell will report an error, all you want to clear.

Two, bash variable scope

  • Descendant process shell process.

    In the shell process was started up again another shell process, the following example is the bash started up again in a bash, bash after the restart so that's, has launched a csh.

    # pstree
    systemd─┬
            ├─sshd───sshd───bash───pstree
    # bash
    # csh
    # pstree
    systemd─┬
            ├sshd───sshd───bash───bash───csh───pstree
  • bash types of variables:

    • Scope divided by a range of variables:

    • Local variables: scope only for the current shell process

      Verify the scope of local variables:

      # firstName=jerry
      # echo $firstName
      jerry
      # csh
      # echo $firstName
      firstName: Undefined variable.
      # exit
      exit
      # echo $firstName
      jerry
      • Assignment: name = value

      • Reference: $ {name}, $ name

        • "": Variable is replaced with the value
        • '': Variable is not replaced by the value
      • View variable: set

      • Undo variable: unset name. Do not pay attention to the former name plus $

        # firstname=tom
        # echo $firstname
        tom
        # unset firstname
        # echo $firstname
        
    • Environment variables: the scope of the current shell process, and their descendants shell process.

      Verification environment variable scope:

      # fn=tom
      # echo $fn
      tom
      # export fn
      # csh
      # echo $fn
      tom
      # exit
      exit
      # echo $fn
      tom
      • Assignment:

        • export name=value

        • name=value

          export name

        • declare -x name=value

        • name=value

          declare -x name

      • Quote: with local variables.

      • Undo variable: unset name. Do not pay attention to the former name plus $

      • bash embedded environment variables:

        PATH,HISTORY,HISTSIZE,HISTFILESIZE,HISTCONTROL,SHELL,HOME,UID,PWD,OLDPWD

      • View the environment variables command:

        export,declare -x,printenv,env

    • Read-only variables (constants): Scope is the current shell process, and do not be revoked. With the termination of the current shell process was terminated.

      • declare -r name
      • readonly name

      Inspection certificate constant scope:

      # la=foo
      # declare -r la
      # echo $la
      foo
      # csh
      # $echo la
      echo: Undefined variable.
      # exit
      exit
      # echo $la
      foo
      # la=aa
      -bash: la: readonly variable
      [root@localhost ~]# unset la
      -bash: unset: la: cannot unset: readonly variable
      • Local variables: function in the variables scope only within the function.
    • Shell script parameters: location parameter variables

    • Special variables:

      • On $ ?: a command execution result. 0: Success; 1-255: Failed.
      • ...
  • Performed together bash, the more command

    • Continuous execute multiple commands:command1;command2;command3...

      These commands will be executed.

    • A plurality of continuous execution command logic

      According to the results of the previous command (success or failure), to decide whether to back the command execution.

      • After the previous command is successful, the latter command before allowing execution:&&

        Example: first check the directory exists or not, the presence before entering this directory

        # pwd
        /root
        # ls /sdf && cd /tmp
        ls: cannot access /sdf: No such file or directory
        # pwd
        /root
        # ls /sdf || cd /tmp
        ls: cannot access /sdf: No such file or directory
        # pwd
        /tmp
      • After the previous command fails, subsequent commands before allowing execution:||

        Example: First a user exists or not, does not exist, create the user; there is not a created.

        # pwd
        /root
        # ls /sdf || cd /tmp
        ls: cannot access /sdf: No such file or directory
        # pwd
        /tmp

c / c ++ mutual learning QQ group: 877 684 253

I micro letter: xiaoshitou5854

Guess you like

Origin www.cnblogs.com/xiaoshiwang/p/12066585.html