栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

shell 中的变量

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shell 中的变量

shell 中的变量

文章目录
  • shell 中的变量
    • 参考文档
    • 用户环境变量的更改
    • Linux 中别名设置
    • 变量的定义
    • 变量定义的方法
    • 变量的转译
    • 变量的数组
    • 传参
    • 脚本函数


参考文档
  • bash 内置变量官方文档:https://www.gnu.org/software/bash/manual/bash.html#Shell-Variables
  • Shell Parameters:https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameters
  • Shell 函数:https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions
  • 参考文章:https://www.runoob.com/linux/linux-shell-variable.html

用户环境变量的更改
  • 环境变量:用户在操作系统时使用到的命令搜索路径。
  • 设定方式
    [root@ ~]#vim ~/.bash_profile
    export PATH=$PATH:/mnt
    
    [root@ ~]#vim /etc/bash_profile
    export PATH=$PATH:/mnt
    

Linux 中别名设置
  • 临时设定
    [root@ shell_variable]# alias xie='vim'
    [root@ shell_variable]# which xie
    alias xie='vim'
            /bin/vim
    [root@ shell_variable]# 
    
  • 只针对用户生效
    [root@ shell_variable]# echo "alias xie='vim'" > ~/.bashrc 
    [root@ shell_variable]# source ~/.bashrc 
    [root@ shell_variable]# which xie
    alias xie='vim'
            /bin/vim
    [root@ shell_variable]#
    
  • 针对系统所有用户
    [root@ shell_variable]# vim /etc/bashrc
    alias xie='vim
    [root@ shell_variable]#
    
  • 删除当前环境中别名
    [root@ shell_variable]# unalias xie
    [root@ shell_variable]#
    
  • 重新加载文件
    [root@ shell_variable]# source ~/.bashrc
    [root@ shell_variable]#
    

变量的定义
  • 定义本身

    • 变量就是内存一片区域的地址。
  • 变量存在的意义

    • 命令无法操作一直变化的目标。
    • 用一串固定的字符来表示不固定的目标可以解决此问题。
  • 变量名称

    • "字符" “_” "数字"
    • 不能用数字开头
    • 建议:
      • 变量名称短全用大写字符
      • 变量名称长用_区分子类
        • WUHAHA
        • Wuhaha_Linux
        • wuhahA_Linux

变量定义的方法
  • 环境级别,在环境关闭后变量失效。

    export a=1
    
  • 用户级别

    vim ~/.bash_profile
    export a=1
    
  • 系统级别

    vim /etc/profile
    export a=2
    
    vim /etc/profile.d/haha.sh
    export b=3
    sh /etc/profile.d/haha.sh
    
  • 【注】:

    • export:声明环境变量。
    • env:显示所有环境变量。
    • echo $PATH:显示变量对应的值。
    • unset variable_name:删除变量,unset 不能删除只读变量。

  • 实例
    [root@ ~]# export a=1
    [root@ ~]# echo $a
    1
    [root@ ~]# env | grep a=1
    a=1
    [root@ ~]# unset a
    [root@ ~]# env | grep a=1
    [root@ ~]# echo $a
    
    [root@ ~]#
    

变量的转译
  • 转译

    • / :转译单个字符
    • " " :弱引用,批量转译个数字符 不能转译 “” 、"`" 、"$" 、"!"。可以出现变量、转译字符。
    • ’ ’ :强引用,单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的。
  • 声明

    [root@ shell_variable]# a=1
    [root@ shell_variable]# echo $a
    1
    [root@ shell_variable]# echo $ab
    
    [root@ shell_variable]# echo ${a}b
    1b
    [root@ shell_variable]#
    

变量的数组
  • 定义数组
    [root@ shell_variable]# a=(1 2 3 4)
    [root@ shell_variable]#
    
  • 打印数组的第一个元素
    [root@ shell_variable]# echo ${a[0]}
    1
    [root@ shell_variable]#
    
  • 打印数组最后一个元素
    [root@ shell_variable]# echo ${a[-1]}
    4
    [root@ shell_variable]#
    
  • 打印所有元素,显示为一串" 1 2 3 4 "
    [root@ shell_variable]# echo ${a[*]}
    1 2 3 4
    [root@ shell_variable]#
    
  • 打印所有元素,显示为一个元素一串。“1” “2” “3” "4"
    [root@ shell_variable]# echo ${a[@]}
    1 2 3 4
    [root@ shell_variable]#
    
  • 0 为起始元素,往后取 3 个。
    [root@ shell_variable]# echo ${a[@]:0:3}
    1 2 3
    [root@ shell_variable]#
    
  • 数组中的元素个数。
    [root@ shell_variable]# echo ${#a[@]}
    4
    [root@ shell_variable]#
    

传参
  • 直接利用命令执行结果

    • $ () | ``:优先执行
    [root@ shell_variable]# TEST=`hostname`
    [root@ shell_variable]# echo $TEST
    wuhaha
    [root@ shell_variable]#
    
    [root@ shell_variable]# TEST=$(hostname)
    [root@ shell_variable]# echo $TEST
    wuhaha
    [root@ shell_variable]#
    
  • 非交互模式 脚本中的传参

    • $0 :脚本本身。
    • $1 :脚本后输入的第一串字符串。
    • $2 :脚本后输入的第二串字符串。
    • $* :脚本后所输入的所有字符"wuhaha linux redhat"。
    • $@ :脚本后所输入的所有字符’westos’ ‘linux’ ‘redhat’。
    • $# :脚本后所输入的字符串个数。
    • #? :上一条命令的退出状态码。" 0 " -> " True "
    • $$ :表示当前进程的id。
    [root@ shell_variable]# cat test.sh 
    #!/bin/bash
    echo $0
    echo $1
    echo $2
    echo $*
    echo $@
    echo $#
    echo $?
    echo $$
    [root@ shell_variable]# sh test.sh wuhaha linux redhat
    test.sh
    wuhaha
    linux
    wuhaha linux redhat
    wuhaha linux redhat
    3
    0
    16404
    [root@ shell_variable]# 
    
  • 非交互式传参

    • read test :对 test 变量赋值。
    • read -p “please input word:” test :输出提示语。
    • read -p “please input password:” -s test :隐藏输入内容。
    [root@ shell_variable]# cat test.sh 
    #!/bin/bash
    read test
    echo "==========="
    echo "echo $test"
    echo "###########"
    read -p "please input word:" test
    echo "==========="
    echo "echo $test"
    echo "###########"
    read -p "please input password:" -s test
    echo -e "necho $test"
    [root@ shell_variable]# sh test.sh 
    this is test.
    ===========
    echo this is test.
    ###########
    please input word:this is test.
    ===========
    echo this is test.
    ###########
    please input password:
    echo this is test.
    [root@ shell_variable]# 
    

脚本函数
  • 定义:程序的别名。
  • 有两种方式,都可以使用。
    [root@ shell_variable]# cat function.sh 
    #!/bin/bash
    function test(){
            echo $1
    }
    test linux
    
    wuha() {
            echo $1
    }
    wuha redhat
    [root@ shell_variable]# sh function.sh 
    linux
    redhat
    [root@ shell_variable]# 
    



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号