6. Shell echo命令

格式

1. echo string
  1. 显示普通字符(双引号可以忽略)

1. echo "hello world" 2. echo hello world 2. 显示转移字符

1. echo "\"hello world\"" 2. echo \"hello world\"

结果:

1. "hello world" 3. 显示变量 read命令从标准输入中读取一行,并把输入行的每个字段的值指定给shell变量:

1. # !/bin/sh 2. read name 3. echo "$name is my name"

将以上代码保存为test.sh,name 接受标准输入的变量,运行结果:

1. asa@asa-virtual-machine:~$ sh hello.sh 2. Tom 3. Tom is my name 4. 显示换行

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

输出结果:

1. OK! 2. It is a test 5. 显示不换行

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

输出结果:

1. OK! It is a test 6. 显示结果定向至文件

1. echo It is a test > myfile 7. 原样输出字符串,不进行转义或取变量(用单引号)

1. echo '$name\"'

输出结果:

1. $name\" 8. 显示命令执行结果

1. echo `date`

注意:这里使用的是 反引号 `,而非单引号。 输出结果显示当前日期:

1. asa@asa-virtual-machine:~$ echo `date` 2. 2016年 01月 22日 星期一 09:14:37 CST