• linux shell变量


    变量的定义:

    通常,作为一种习惯约定,环境变量声明全部使用大写。

    简单的定义只需要=就可以,这种定义的类型只能是字符串,如果要定义其他类型的变量,需要使用declare。

    testv="this is a test virable"

    这种方式是定义字符串。整个语句都不能有空格,因为一旦有空格就会作为命令来执行,如果变量的内容有空格,那么就用引号括起来。

    [root@localhost ~]# testv = 123

    bash: testv: 未找到命令...

    相似命令是: 'test'

    [root@localhost ~]# testv= 123

    bash: 123: 未找到命令...

    ^[[A^[[A^C

    [root@localhost ~]# testv=123

    [root@localhost ~]# echo $testv

    123

    $var或者${var}可以引用其他变量的值。

    $(command)可以引用其他命令的结果。

    如下:

    [root@localhost ~]# testv=$PATH

    [root@localhost ~]# echo $testv

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    [root@localhost ~]# testv=$(pwd)

    [root@localhost ~]# echo $testv

    /root

     

    declare定义其他类型:

    -a 定义array

    -i 定义integer

    -x 定义环境变量

    -r 定义只读变量

    定义integer:

    [root@localhost ~]# testi=10+20

    [root@localhost ~]# echo $testi

    10+20

    [root@localhost ~]# declare -i testi=10+20

    [root@localhost ~]# echo $testi

    30

     

    [root@localhost ~]# declare -i vari

    [root@localhost ~]# vari=1+1

    [root@localhost ~]# echo $vari

    2

    定义数组的第一种方式,直接通过下标:

    [root@localhost ~]# arr[2]=3

    [root@localhost ~]# echo ${arr[2]}

    3

    [root@localhost ~]# echo ${arr[0]}

    第二种方式:

    [root@localhost ~]# declare -a arr

    取消变量:

    [root@localhost ~]# unset testv

    [root@localhost ~]# echo $testv

    打印变量

    echo是打印字符串到控制台的命令,可以给一个带引号(单引号或者双引号)的文本参数。结果是一样的。

    如下:

    [root@localhost ~]# echo jjj

    jjj

    [root@localhost ~]# echo "jjj"

    jjj

    [root@localhost ~]# echo 'jjj'

    jjj

    但是在打印变量的时候,这三个的表现却不一样。

    最简单的方式:

    [root@localhost ~]# echo $PATH

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    另外一种方式:

    [root@localhost ~]# echo ${PATH}

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    为什么需要第二种方式,请看下面:

    [root@localhost ~]# echo $PATHotherstr

     

    [root@localhost ~]# echo ${PATH}otherstr

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binotherstr

    单引号和双引号的区别:

    [root@localhost ~]# echo "$PATH"

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    [root@localhost ~]# echo '$PATH'

    $PATH

    使用双引号来解决拼接:

    [root@localhost ~]# echo "$PATH"otherstr

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binotherstr

    变量的作用域:

    [root@localhost testsh]# touch testvirable.sh

    [root@localhost testsh]# vim testvirable.sh

     

    #测试变量作用域

    testv="this is a test virable"

    echo "print testv inner sh file :"

    echo "$testv"

    [root@localhost testsh]# ./testvirable.sh

    -bash: ./testvirable.sh: 权限不够

    [root@localhost testsh]# chmod 775 testvirable.sh

    [root@localhost testsh]# ./testvirable.sh

    print testv inner sh file :

    this is a test virable

    [root@localhost testsh]# sh testvirable.sh

    print testv inner sh file :

    this is a test virable

    [root@localhost testsh]# bash testvirable.sh

    print testv inner sh file :

    this is a test virable

    [root@localhost testsh]# echo $testv

    ./和sh和bash都是执行bash脚本的命令,实际上都是重新开启了一个子进程来执行shell脚本,子进程的内存和父进程不是共享的。所以在子进程定义的变量,在父进程根本看不到。

    使用export之后的效果:

    [root@localhost testsh]# vim testvirable.sh

     

    #测试变量作用域

    testv="this is a test virable"

    echo "print testv inner sh file :"

    echo "$testv"

    export testv

    [root@localhost testsh]# bash testvirable.sh

    print testv inner sh file :

    this is a test virable

    [root@localhost testsh]# echo $testv

    在子进程中执行export之后,父进程仍然看不到。

    在父进程执行export之后的效果:

    [root@localhost testsh]# vim testvirable.sh

     

    #测试变量作用域

    echo "print testv inner sh file :"

    echo "$testv"

    [root@localhost testsh]# testv="this is a test virable"

    [root@localhost testsh]# echo $testv

    this is a test virable

    [root@localhost testsh]# export testv

    [root@localhost testsh]# export | grep testv

    declare -x testv="this is a test virable"

    [root@localhost testsh]# bash testvirable.sh

    print testv inner sh file :

    this is a test virable

    可见,在父进程export的变量在子进程可见。

    不同的shell脚本执行方式

    • 通过sh命令
    • 或者bash命令
    • 或者相对路径./
    • 绝对路径(使用绝对路径不用加./)

    以上四种都是通过新建一个子进程的方式来执行shell脚本的。

    source命令:在当前进程执行shell脚本。

    [root@localhost ~]# echo $testv

     

    [root@localhost ~]# testv="this is a test virable"

    [root@localhost ~]# echo $testv

    this is a test virable

    [root@localhost ~]# cd /testsh/

    [root@localhost testsh]# source testvirable.sh

    print testv inner sh file :

    this is a test virable

    可以看到,不需要export,也可以打印出变量。

    注意source是内嵌命令:

    [root@localhost testsh]# type -a bash

    bash 是 /usr/bin/bash

    [root@localhost testsh]# type -a sh

    sh 是 /usr/bin/sh

    [root@localhost testsh]# type -a source

    source 是 shell 内嵌

    环境变量

    查看环境变量的两种方式:

    [root@localhost ~]# env | head -n 3

    XDG_SESSION_ID=19

    HOSTNAME=localhost.localdomain

    SELINUX_ROLE_REQUESTED=

    [root@localhost ~]# export | head -n 3

    declare -x HISTCONTROL="ignoredups"

    declare -x HISTSIZE="1000"

    declare -x HOME="/root"

    查看所有变量:环境变量和普通变量

    [root@localhost ~]# set | head -n 3

    BASH=/bin/bash

    BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath

    BASH_ALIASES=()

    set中有两个特殊的系统变量,一个是$,代表当前shell环境的进程id,一个是?,代表上一个命令的返回结果码(0表示成功)。

    [root@localhost ~]# echo $$

    6073

    [root@localhost ~]# echo $?

    0

    export

    将普通变量转换为环境变量。

    在子进程中有效。

    每次登陆之后启动的shell环境,是一个新的进程。所以不会有上次设定的环境变量。

    如PATH这种环境变量,每次都会有,是因为在初始化shell环境的时候,会执行类似初始化脚本的操作。初始化这些环境变量。

    在/etc/ profiles文件中的内容会在每个用户登录的时都生效,在~/ .bashrc下的内容,只会在当前用户登录才生效。

    在里面,都可以加入

    export var=value

    来增加环境变量。

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Smart Client Architecture and Design Guide
    Duwamish密码分析篇, Part 3
    庆贺发文100篇
    .Net Distributed Application Design Guide
    New Introduction to ASP.NET 2.0 Web Parts Framework
    SPS toplevel Site Collection Administrators and Owners
    来自Ingo Rammer先生的Email关于《Advanced .Net Remoting》
    The newsletter published by Ingo Rammer
    深度探索.Net Remoting基础架构
    信道、接收器、接收链和信道接受提供程序
  • 原文地址:https://www.cnblogs.com/xiaolang8762400/p/7368097.html
Copyright © 2020-2023  润新知