首页 > 系统服务 > 详细

shell中的变量

时间:2019-02-20 21:03:47      阅读:211      评论:0      收藏:0      [点我收藏+]

一、系统变量

常用系统变量

$HOME
$PWD
$SHELL
$USER

 查看变量值

[root@slave2 testshell]# echo $HOME
/root
[root@slave2 testshell]# echo $PWD
/root/testshell
[root@slave2 testshell]# echo $USER
root

 显示当前Shell中所有变量

[root@slave2 testshell]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION=4.2.46(2)-release
CLASSPATH=.::/usr/local/src/jdk1.8.0_171/lib
COLUMNS=119
DIRSTACK=()
EUID=0
GROUPS=()
HADOOP_HOME=/usr/local/src/hadoop-2.6.1
HBASE_HOME=/usr/local/src/hbase-0.98.6-hadoop2
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=slave2
HOSTTYPE=x86_64
ID=0
IFS=$ \t\n
JAVA_HOME=/usr/local/src/jdk1.8.0_171
KAFKA_HOME=/usr/local/src/kafka_2.11-0.10.2.1
LANG=en_US.UTF-8
LESSOPEN=||/usr/bin/lesspipe.sh %s
LINES=61
LOGNAME=root

二、自定义变量

 1. 基本语法

  • 定义变量:变量=值(=两边没有空格)
    [root@slave2 testshell]# W=10
    [root@slave2 testshell]# echo $W
    10
  • 撤销变量:unset 变量
    [root@slave2 testshell]# W=10
    [root@slave2 testshell]# echo $W
    10
    [root@slave2 testshell]# unset W
    [root@slave2 testshell]# echo $W
    
    [root@slave2 testshell]#
  • 声明静态变量:readonly 变量,注意不能unset
    [root@slave2 testshell]# readonly B=2
    [root@slave2 testshell]# echo $B
    2
    [root@slave2 testshell]# unset B
    -bash: unset: B: cannot unset: readonly variable

 2.变量定义规则

  • 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写
  • 等号两边不能有空格
  • 在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
    [root@slave2 testshell]# C=1+1
    [root@slave2 testshell]# echo C
    C
    [root@slave2 testshell]# echo $C
    1+1
  • 变量的值如果有空格,需要使用“”或‘’括起来
    [root@slave2 testshell]# D=banzhang love mm
    -bash: love: command not found
    [root@slave2 testshell]# D=banzhang love mm
    [root@slave2 testshell]# echo $D             
    banzhang love mm
  • 可把变量提升为全局环境变量,可供其它shell程序使用
    [root@slave2 testshell]# D=banzhang love mm
    [root@slave2 testshell]# echo $D             
    banzhang love mm
    [root@slave2 testshell]# vim ../helloworld.sh 
    #!/bin/bash
    echo hello world!
    echo $D
    ~                                                                                                                      
    ~                                                                                                                      
    "../helloworld.sh" 3L, 40C written                                                                 
    [root@slave2 testshell]# ../helloworld.sh 
    hello world!
    
    [root@slave2 testshell]# export D
    [root@slave2 testshell]# ../helloworld.sh 
    hello world!
    banzhang love mm

三、特殊变量

3.1 特殊变量:$n

  • 基本语法

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

 

shell中的变量

原文:https://www.cnblogs.com/zxbdboke/p/10409117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!