1.hello word
#!/bin/bash
echo "hello word"
2. 变量
#!/bin/bash
#变量
my_name="Mr.zhang"
echo $my_name
echo ${my_name}
#只读变量
sex="man"
age="20"
readonly sex
sex="woman"
age="21"
echo $sex
echo $age
执行结果:
3. 删除变量
#!/bin/bash
#删除变量
name="Mr.zhang"
score="30"
unset score
echo $name
echo $score
执行结果:
4. 环境变量
#!/bin/bash
echo $PATH
echo $HOME
echo $LOGNAME
执行结果
5. 字符串
#!/bin/bash
#字符串:字符串可以用单引号,也可以用双引号,也可以不用引号。
string1=Without_the_quotes
string2='single quotes'
string3="double qutation marks"
echo ${string1}
echo $string2
echo $string3
#不加引号字符之间不能带空格
string4=Without the quotes
echo $string4
执行结果
6. 字符串
#!/bin/bash
#字符串:单引号、双引号
#单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的。
#双引号里可以有变量,双引号里可以出现转义字符
name=Mr.zhang
str1='I know you are \"$name\" '
str2="I know you are \"$name\" "
echo $str1
echo $str2
执行结果:
7. 字符串
#!/bin/bash
#字符串拼接:单引号、双引号;引号拼接成对出现;
name=Mr.zhang
age=18
str1="name=$name"" age=$age"
str2='name=$name'' age=&age'
str3='name='$name' age='$age
echo $str1
echo $str2
echo $str3
#成对引号之间不能有空格,相当于无引号赋值
str5=Mr.zhang 18
str6='Mr.zhang' '18'
echo str5
echo str6
执行结果: