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 表示第一個參數
#!/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
#!/bin/bash
#變量的使用
echo -e "\033[32m###################\033[0m"
echo "The \$? is $?"
echo "The \$* is $*"
echo "The \$# is $#"
echo "The \$1 is $1"
echo "The \$2 is $2"