Shell echo命令

1.显示普通字符串:

echo "It is a test"

这里的双引号完全可以省略,以下命令与上面实例效果一致:

echo It is a test

2.显示转义字符

echo ""It is a test""

结果将是:

"It is a test"



3.显示变量

参数说明:

  •  -p 输入提示文字
  •  -n 输入字符长度限制(达到6位,自动结束)
  •  -t 输入限时
  •  -s 隐藏输入内容

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

#!/bin/sh
read name 
echo "$name It is a test"

以上代码保存为 test.sh,name 接收标准输入的变量,

结果将是:

[root@www ~]# sh test.sh

OK                     #标准输入
OK It is a test        #输出



例子test.sh:
read firstStr secondStr
echo "第一个参数:$firstStr; 第二个参数:$secondStr"

结果输出:

$ sh test.sh 
一 二 三 四
第一个参数:一; 第二个参数:二 三 四

例子test.sh

read -p "请输入一段文字:" -n 6 -t 5 -s password
echo -e "
password is $password"

结果输出:

$ sh test.sh 
请输入一段文字:
password is asdfgh


4.显示换行

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

输出结果:

OK!

It is a test




5.显示不换行

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

输出结果:

OK! It is a test




6.显示结果定向至文件

echo "It is a test" > myfile



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

echo '$name"'

输出结果:

$name"




8.显示命令执行结果

echo `date`

注意: 这里使用的是反引号 `, 而不是单引号 '。

结果将显示当前日期

Thu Jul 24 10:08:46 CST 2014


原文地址:https://www.cnblogs.com/sea-stream/p/9883015.html