zoukankan      html  css  js  c++  java
  • Shell 中的变量

    一、系统变量

    1、常用系统变量

    $HOME、$PWD、$SHELL、$USER等

    2、案例实操

    (1)查看系统变量的值

    [root@centos7 shell_test]# echo $HOME
    /root

    (2)显示当前Shell中所有变量:set

    [root@centos7 shell_test]# set
    
    大致结构如下:
    BASH=/bin/bash
    BASH_ALIASES=()
    BASH_ARGC=()
    BASH_ARGV=()

    二、自定义变量

    1、基本语法

    1. 定义变量:变量=值
    2. 撤销变量:unset 变量
    3. 声明静态变量:readonly变量,注意:不能unset

    2、变量定义规则

    1. 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
    2. 等号两侧不能有空格
    3. 在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
    4. 变量的值如果有空格,需要使用双引号或单引号括起来。

    3、案例实操

    (1)定义变量A

    [root@centos7 shell_test]# A=5
    [root@centos7 shell_test]# echo $A
    5

    (2)给变量A重新赋值

    [root@centos7 shell_test]# A=8
    [root@centos7 shell_test]# echo $A
    8

    (3)撤销变量A

    [root@centos7 shell_test]# unset A
    [root@centos7 shell_test]# echo $A

    (4)声明静态的变量B=2,不能unset

    [root@centos7 shell_test]# readonly B=2
    [root@centos7 shell_test]# echo $B
    2
    [root@centos7 shell_test]# B=9
    -bash: B: readonly variable

    (5)在bash中,变量默认类型都是字符串类型,无法直接进行数值运算

    [root@centos7 shell_test]# C=1+2
    [root@centos7 shell_test]# echo $C
    1+2

    (6)变量的值如果有空格,需要使用双引号或单引号括起来

    [root@centos7 shell_test]# D=I love banzhang
    -bash: world: command not found
    [root@centos7 shell_test]# D="I love banzhang"
    [root@centos7 shell_test]# echo $A
    I love banzhang

    (7)可把变量提升为全局环境变量,可供其他 Shell 程序使用

    export 变量名

    [root@centos7 shell_test]# vim helloworld.sh
    
    
    在helloworld.sh文件中增加echo $B
    #!/bin/bash
    echo "helloworld"
    echo $B
    
    
    [root@centos7 shell_test]# ./helloworld.sh
    Helloworld
    
    发现并没有打印输出变量B的值。
    [root@centos7 shell_test]# export B
    [root@centos7 shell_test]# ./helloworld.sh
    helloworld
    2

    三、特殊变量:$n

    1、基本语法

    $n(功能描述:n为数字,$0代表该脚本名称,$1-$9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如${10})

    2、案例实操

    输出该脚本文件名称、输入参数1和输入参数2 的值

    [root@centos7 shell_test]# touch parameter.sh 
    [root@centos7 shell_test]# vim parameter.sh
    
    #!/bin/bash
    echo "$0  $1   $2"
    
    [root@centos7 shell_test]# chmod 777 parameter.sh
    [root@centos7 shell_test]# ./parameter.sh cls  xz
    ./parameter.sh  cls   xz

    四、特殊变量:$#

    1、基本语法

    $#(功能描述:获取所有输入参数个数,常用于循环)

    2、案例实操

    获取输入参数的个数

    [root@centos7 shell_test]# vim parameter.sh
    #!/bin/bash
    echo "$0  $1   $2"
    echo $#
    
    [root@centos7 shell_test]# chmod 777 parameter.sh
    [root@centos7 shell_test]# ./parameter.sh cls  xz
    parameter.sh cls xz 
    2

    五、特殊变量:$*、$@

    1、基本语法

    • $*(功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)
    • $@(功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)

    2、案例实操

    打印输入的所有参数

    [root@centos7 shell_test]# vim parameter.sh
    
    #!/bin/bash
    echo "$0  $1   $2"
    echo $#
    echo $*
    echo $@
    
    [root@centos7 shell_test]# bash parameter.sh 1 2 3
    parameter.sh  1   2
    3
    1 2 3
    1 2 3

    六、特殊变量:$?

    1、基本语法

    $?(功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了)

    2、案例实操

    [root@centos7 shell_test]# ./helloworld.sh 
    hello world
    [root@centos7 shell_test]# echo $?
    0
  • 相关阅读:
    Flink 作为现代数据仓库的统一引擎:Hive 集成生产就绪!
    终于要跟大家见面了,Flink 面试指南
    了解jQuery的$符号
    关于serialVersionUID的说明
    Java类更改常量后编译不生效
    ora-00054资源正忙,但指定以nowait方式
    【Servlet】基于Jsp的微信Oauth2认证
    [Maven]Maven构建可执行的jar包(包含依赖jar包)
    FTP服务FileZilla Server上传提示550 Permission denied
    nginx
  • 原文地址:https://www.cnblogs.com/jwen1994/p/14894543.html
Copyright © 2011-2022 走看看