变量定义的=前后不能有空格
variable=value
name=sarath
age=28
使用$访问变量
echo $variable
echo $name
echo $age
使用read函数
#!/bin/bash
echo "what is your name?"
read name
echo "Hello $name!. Welcome to Slashroot Scripting Tutorial"
执行以上脚本首先执行“what is your name”、然后等待用户输入。
把命令直接放到双引号中,就OK了
#!/bin/bash
DATE=`date`
echo "Today is $DATE"
0表示脚本本身, 0 表 示 脚 本 本 身 , 1,表示第一个变量,$2表示第二个变量。依次类推
#!/bin/bash
firstname=$1
lastname=$2
scriptname=$0
echo "Hello!. So your Firstname is $firstname, and your Lastname is $lastname and the script that you are executing is $scriptname"
或者:
#!/bin/bash
echo "Hello!. So your Firstname is $1, and your Lastname is $2 and the script that you are executing is $0"
脚本执行:
# ./test1.sh Sarath Pillai
每一个命令在shell脚本里执行成功后返回一个0-255的状态数字。0表示执行成功,其他非零表示执行失败。
这些值会存放在 $?里
root@localhost:/opt# ls
testdirectory
root@localhost:/opt# echo $?
0
root@localhost:/opt# ls )
-bash: syntax error near unexpected token `)'
ubuntu@ip-10-12-4-160:/opt$ echo $?
2
使用unset
#!/bin/bash
variable=2
echo The variable value is $variable
unset variable
echo The variable value is $variable
https://www.slashroot.in/variables-linux-shell-scripts-explained-examples