赞
踩
目录
- [root@admin-wolf ~]# /bin/echo --help
- Usage: /bin/echo [SHORT-OPTION]... [STRING]...
- or: /bin/echo LONG-OPTION
- Echo the STRING(s) to standard output.
-
- -n do not output the trailing newline
- -e enable interpretation of backslash escapes
- -E disable interpretation of backslash escapes (default)
- --help display this help and exit
- --version output version information and exit
-
- If -e is in effect, the following sequences are recognized:
-
- \\ backslash
- \a alert (BEL)
- \b backspace
- \c produce no further output
- \e escape
- \f form feed
- \n new line
- \r carriage return
- \t horizontal tab
- \v vertical tab
- \0NNN byte with octal value NNN (1 to 3 digits)
- \xHH byte with hexadecimal value HH (1 to 2 digits)
-
- NOTE: your shell may have its own version of echo, which usually supersedes
- the version described here. Please refer to your shell's documentation
- for details about the options it supports.
- GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
- For complete documentation, run: info coreutils 'echo invocation'
- You have mail in /var/spool/mail/root
- [root@admin-wolf ~]# /bin/echo --version
- echo (GNU coreutils) 8.22
- Copyright (C) 2013 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
- This is free software: you are free to change and redistribute it.
- There is NO WARRANTY, to the extent permitted by law.
- Written by Brian Fox and Chet Ramey.
注意:如果直接echo --help、echo --version是不显示以上结果的。
命令格式:echo [-neE] 内容
echo参数详解
-n :使用此参数时,不会进行自动换行,包含换行符也不会进行换行,是直接输出
-e :使用此参数,对特殊转义字符进行不一样的解释
\\ 插入反斜杠字符
\a 发出警报声(BEL)
\b 显示退格字符
\c 抑制任何进一步输出,输出\c 前面的字符
\e 显示转义字符
\f 显示换页符
\n 显示新行
\r 显示回车,显示\r 后面的字符
\t 显示水平选项卡
\v 显示垂直选项卡
-E :禁止转义字符解释,默认值
结合以下示例:
- [root@admin-wolf ~]# echo -e "oooo\\ppppp\\qqqqqq"
- oooo\ppppp\qqqqqq
- [root@admin-wolf ~]# echo -e "oooo\apppppqqqqqq"
- oooopppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\bppppp\bqqqqqq"
- oooppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\cpppppqqqqqq"
- oooo
- [root@admin-wolf ~]# echo -e "oooo\fpppppqqqqqq"
- oooo
- pppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\nppppp\nqqqqqq"
- oooo
- ppppp
- qqqqqq
- [root@admin-wolf ~]# echo -e "oooo\rppppp\rqqqqqq"
- qqqqqq
- [root@admin-wolf ~]# echo -e "oooo\rpppppqqqqqq"
- pppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\tpppppqqqqqq"
- oooo pppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\vpppppqqqqqq"
- oooo
- pppppqqqqqq
- [root@admin-wolf ~]# echo -e "oooo\tppppp\tqqqqqq"
- oooo ppppp qqqqqq
- You have mail in /var/spool/mail/root
- [root@admin-wolf ~]# echo -e "oooo\vppppp\vqqqqqq"
- oooo
- ppppp
- qqqqqq
[root@admin-wolf ~]# echo "hello"
hello
[root@admin-wolf ~]# echo 'hello'
hello
[root@admin-wolf ~]# echo hello
hello
双引号包含双引号
[root@admin-wolf ~]# echo "hello "linux""
hello linux双引号包含双引号转义
[root@admin-wolf ~]# echo "hello \"linux\""
hello "linux"单引号包含双引号
[root@admin-wolf ~]# echo 'hello "linux"'
hello "linux"单引号包含单引号
[root@admin-wolf ~]# echo 'hello 'linux' '
hello linux单引号包含单引号转义
[root@admin-wolf ~]# echo $'hello \'linux\''
hello 'linux'
[root@admin-wolf ~]# echo -e "o\ro\boo\\pppp\fqqq\nqqll\tl\vl\chhhhh"
oo\pppp
qqq
qqll l
l
[root@admin-wolf ~]# echo "hello linux " >a.txt
[root@admin-wolf ~]# cat a.txt
hello linux[root@admin-wolf ~]# echo "hello linux 123qwer" >> a.txt
[root@admin-wolf ~]# cat a.txt
hello linux
hello linux 123qwer
注意:>是覆盖原来的文件内容 ; >>是在原有内容下最后一行开始追加写入内容
[root@admin-wolf ~]# echo $USER
root
[root@admin-wolf ~]# echo "Today is :$(date)"
Today is :Fri Nov 4 16:34:04 CST 2022
[root@admin-wolf ~]# echo "Today is :$(date +%D)"
Today is :11/04/22
echo可以修改字体类型、字体背景、字体颜色,通过转义序列 \033且必须使用 -e 参数。
# echo -e "\033[字背景颜色;字体颜色;字体属性m 需要输出的内容 \033[0m"
字体背景色 范围:40-47
字体颜色 范围:30-37
m 转义终止符号,颜色指定
再次使用\033[ 开启颜色定义,0表示定义结束,\033[0m 是恢复之前配色方案
1)字体控制选项
\e[0m
关闭所有属性\e[1m
设置高亮度\e[4m
下划线\e[5m
闪烁\e[7m
反显,撞色显示,显示为白字黑底,或者显示为黑底白字\e[8m
消影,字符颜色将会与背景颜色相同\e[nA
光标上移 n 行\e[nB
光标下移 n 行\e[nC
光标右移 n 行\e[nD
光标左移 n 行\e[y;xH
设置光标位置\e[2J
清屏\e[K
清除从光标到行尾的内容\e[s
保存光标位置\e[u
恢复光标位置\e[?25
隐藏光标\e[?25h
显示光标色彩 | 黑 | 红 | 绿 | 黄 | 蓝 | 洋红 | 青 | 白 |
---|---|---|---|---|---|---|---|---|
前景色 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 |
背景色 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
使用tput命令会使用terminfo数据库中的信息,控制改变终端的控制光标、文本属性、控制屏幕以及文本涂色
修改文本涂色命令
tput setab 用于设置背景色
tput setaf 用于设置前景色
tput sgr0 颜色重置为默认颜色
颜色定义表
数值 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
颜色 | 黑色 | 红色 | 绿色 | 黄色 | 蓝色 | 洋红色 | 黄色 | 白色 |
示例(实际测试时颜色会改变):
- [root@admin-wolf ~]# tput setaf 2
- [root@admin-wolf ~]# tput setaf 3
- [root@admin-wolf ~]# tput sgr0
- [root@admin-wolf ~]# tput setab 3
- [root@admin-wolf ~]# tput setab 2
- [root@admin-wolf ~]# tput sgr0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。