shell 变量

shell 变量

#!/bin/bash
A=123
echo $A
#!/bin/bash
name=张三
echo "This is my name:$name"

执行结果

This is my name:张三

获取环境变量

#!/bin/bash
echo $UID
echo $PWD

结果

0
/home/shell

UID为0表示root用户!

#!/bin/bash
echo $UID
echo $PWD

echo  "###################"
echo $0
echo $1

输出结果:

# /bin/bash var.sh hello
0
/home/shell
###################
var.sh
hello

其中$0 表示文件本身 $1 表示第一个参数

$? 表示上一条命令执行是否成功,成功为0
$* 表示所有的参数
$# 表示参数的个数

#!/bin/bash
#变量的使用
echo  "###################"
echo "The $? is $?"
echo "The $* is $*"
echo "The $# is $#"
echo "The $1 is $1"
echo "The $2 is $2"

结果如下:

# /bin/bash var.sh hello world
###################
The $? is 0
The $* is hello world
The $# is 2
The $1 is hello
The $2 is world

echo 用于打印信息,友情提示。
有颜色的打印:

#!/bin/bash
#变量的使用
echo -e "33[32m###################33[0m"

echo "The $? is $?"
echo "The $* is $*"
echo "The $# is $#"
echo "The $1 is $1"
echo "The $2 is $2"
原文地址:https://www.cnblogs.com/jiqing9006/p/10028680.html