一.Echo输出方式
1.1
2.echo 后面直接跟字符串;
[root@bogon tmp]# echo hello world
hello world
[root@bogon tmp]# echo hello,world ;you are welcome
hello,world
-bash: you: command not found
[root@bogon tmp]# echo hello,welcome!
hello,welcome!
注解:echo后面直接输出字符串,如果遇到分号,默认语句结束,bash不能识别后面的you,所以报错,-bash:you:command not found;要正确输出需要加引号,把分号括在里面。
1.2
echo 后面跟单引号,输出字符串。
[root@bogon tmp]# echo 'hello world ; you are welcome!';
hello world ; you are welcome!
[root@bogon tmp]# echo 'this is my home $HOME'
this is my home $HOME
[root@bogon tmp]# echo 'this is my pwd'
this is my pwd
单引号引用起来的不管是命令,还是变量,都被shell看做是普通字符串,原样输出。
1.3
echo 后面跟双引号,输出字符串。
[root@bogon tmp]# echo "this is another quote,my home is $HOME;"
this is another quote,my home is /root;
[root@bogon tmp]# echo "hello world ,you are welcome "
hello world ,you are welcome
[root@bogon tmp]# echo "hello ,world ! you are welcome ."
hello ,world ! you are welcome .
[root@bogon tmp]# echo "hello ,world ! you are welcome!"
-bash: !": event not found
双引号支持变量展开输出,但是不知道为什么,如果引用的字符串最后面一个字符是感叹号的话就会报错,是不是因为感叹号是shell的关键字?如果写在单独的文件中,进行执行又不会报错。
touch echo.txt
vi echo.txt
顶格输入
#!/bin/bash
echo “hello world !”
chmod a+x echo.txt
cd echo.txt目录
[root@bogon tmp]# ./echo.txt
hello world !
1.4
echo 后面也可以直接跟反引号,反引号只能引用命令。
[root@bogon tmp]# echo `id`
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@bogon tmp]# echo `pwd`
/tmp
1.5
echo 加 -e 可以引用转义字符。
man echo
DESCRIPTION 使用说明
Echo the STRING(s) to standard output. 输出字符串到标准输出。
-n do not output the trailing newline
加 -n 不会输出结尾的换行符
-e enable interpretation of backslash escapes
加 -e 能够解释反斜线的转义使用
-E disable interpretation of backslash escapes (default)
加 -E 禁用反斜线的转义使用(默认)
--help display this help and exit
显示帮助并退出
If -e is in effect, the following sequences are recognized:
如果加-e 选项效果,如下的序列会被识别。
\\ backslash
\a alert (BEL)
\b backspace
\t horizontal tab 水平制表符
\v vertical tab 垂直制表符
\n new line 新行
[root@bogon tmp]# echo -e "aa\t bb\tcc\tdd"
aa bb cc dd
[root@bogon tmp]# echo -e "aa\v bb\vcc\vdd"
aa
bb
cc
Dd
[root@bogon tmp]# echo -e 'hello,this is my home.\n this is your home. \n this is her home.'
hello,this is my home.
this is your home.
this is her home.
1.6
echo 加-e 可以使输出带颜色。
1.61 "\e[1;31m" 是颜色输出启用转义字符,1表示启用颜色;
1.62 "\e[0m" 是颜色输出复位转义字符,0表示复位颜色;
背景色可以被以下数字替换
0 透明(使用终端颜色), 40 黑, 41 红, 42 绿, 43 黄, 44 蓝 45 紫, 46 青绿, 47白(灰)
前景色(也就是文本的颜色)可以被以下数字替换
30 黑 31 红, 32 绿, 33 黄, 34 蓝, 35 紫, 36 青绿, 37 白(灰)
[root@bogon tmp]# echo -e "\e[1;31m this is dog \e[0m"
this is dog
[root@bogon tmp]# echo -e "\e[1;31m this is dog \e[0m"
this is dog