linux 中 echo命令

 

linux 中echo命令用于各种形式的字符串输出。

转义字符 含义
\b 删除前一个字符
\n 换行
\t 水平制表符(tab)
\v 垂直制表符(tab)
\c \c后面的字符将不会输出,输出完毕后也不会换行
\r 光标移动到首行,换行
\f 换行,光标停在原处
\e 删除后一个字符
\ 输出\
\0nnn 输出八进制nnn代表的ASCII字符
\xHH 输出十六进制数HH代表的ASCII字符
\a 输出一个警告的声音

001、基本用法

[root@PC1 test01]# echo "linux"
linux

 

002、\b:删除前一个字符

[root@PC1 test01]# echo "linux"          ## 直接输出
linux
[root@PC1 test01]# echo "lin\bux"
lin\bux
[root@PC1 test01]# echo -e "lin\bux"     ## 删除前一个字符,需要-e的配合
liux

 

003、\n 表示换行

[root@PC1 test01]# ls
[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "linu\nx"       ## \n表示换行
linu
x

 

004、\t 表示水平制表符

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "linu\tx"       ## \t表示水平制表符
linu    x

 

005、\v表示垂直制表符

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "linu\vx"     ## -v 垂直制表符
linu
    x

 

006、 \c后面的字符将不会输出,输出完毕后也不会换行

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "li\cnux"    ## \c后面的字符将不会输出,输出完毕后也不会换行
li[root@PC1 test01]#

 

007、\r 光标移动到首行,换行

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "lin\rux"
uxn
[root@PC1 test01]# echo -e "l\rinux"
inux

 

008、\f 换行,光标停在原处

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -e "linu\fx"
linu
x
[root@PC1 test01]# echo -e "li\fnux"
li
nux

 

009、\e 删除后一个字符

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo "linu\ex"
linu\ex
[root@PC1 test01]# echo -e "linu\ex"       ## 删除后一个字符
linu
[root@PC1 test01]# echo -e "l\einux"
lnux

 

010、\a: 输出警告声

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo "linux\a"
linux\a
[root@PC1 test01]# echo -e "linux\a"       ## 输出警告声
linux

 

011、-n表示不换行输出

[root@PC1 test01]# echo "linux"
linux
[root@PC1 test01]# echo -n "linux"     ## -n表示不换行输出
linux[root@PC1 test01]#

 

012、输出变量值

[root@PC1 test01]# var=abcdefg
[root@PC1 test01]# echo $var
abcdefg
[root@PC1 test01]# echo ${var}       ## 输出变量值
abcdefg

 

013、输出变量字符串的长度

[root@PC1 test01]# echo ${var}
abcdefg
[root@PC1 test01]# echo ${#var}         ## 输出变量字符串的长度
7

 

014、截取前几个字符

[root@PC1 test01]# echo ${var}
abcdefg
[root@PC1 test01]# echo ${var:0:3}    ## 截取前三个字符
abc

 

015、从指定位置截取

[root@PC1 test01]# echo ${var}
abcdefg
[root@PC1 test01]# echo ${var:3:3}      ## 从第四个字符开始,向后截取3个字符
def

 

参考:https://blog.csdn.net/qq_16268979/article/details/109553410

 

posted @ 2023-06-22 10:11  小鲨鱼2018  阅读(879)  评论(0编辑  收藏  举报