Shell编程入门二:echo命令

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

Shell的 echo 命令和PHP的echo指令类似,都是用于输出值。我们可以使用 echo 实现更复杂的输出格式控制。

显示普通字符串

echo "Hello World"
echo Hello World

这两句输出结果均为:Hello World

显示转义字符

echo "\"Hello World\""
echo \"Hello World\"

这两句输出结果均为:“Hello World”

显示变量

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给shell变量。

read name
echo "Your input is $name"

name 接收标准输入的变量。

显示换行

echo -e "OK! \n" # -e 开启转义
echo "It is a test"

输出结果:
1

显示不换行

echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"

输出结果:
2

显示结果定向至文件

echo "It is a test" > myfile

原样输出字符串,不进行转义或取变量(单引号)

echo '$name\"'

输出结果为:$name\”

显示命令执行结果

echo `date`

结果将显示当前日期:
3


参考自:http://www.runoob.com/linux/linux-shell-echo.html

猜你喜欢

转载自blog.csdn.net/transformer_WSZ/article/details/79285477