echo&$ 使用注意事项

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33656602/article/details/81604628

构建基本脚本

echo 显示消息

echo 命令可用单引号或双引号或不用引号来划定字符串,但若在字符串中使用到了单引号或双引号,则需要在字符串中使用一种引号,而用另一种引号将字符串划定起来。
- 字符串中无单引号或双引号

#echo string
string
  • 字符串中有单引号

不用双引号划定字符串而直接输出

#echo str'ing
> 

使用双引号划定字符串

#echo "str'ing"
str'ing
  • 字符串中有双引号

不用单引号划定字符串而直接输出

#echo str"ing
> 

用单引号划定字符串

#echo 'str"ing'
str"ing

$使用变量

只要脚本在引号中出现美元符($),则shell会认为这是在引用一个变量。

  • 例子
#echo "The cost of the item is $15"
The cost of the item is 5

shell 会尝试显示变量$1(但未定义),再显示数字5

  • 显示美元符的解决办法
    在美元符($)前防止一个反斜线
#echo "The cost of the item is \$15"
The cost of the item is $15

猜你喜欢

转载自blog.csdn.net/qq_33656602/article/details/81604628
今日推荐