$ num=8 # 方法1:双引号 $ text="There are $num ducks swimming in a pond" $ echo$text There are 8 ducks swimming in a pond $ $ text="There are "$num" ducks swimming in a pond" $ echo$text There are 8 ducks swimming in a pond
# 方法2:单引号 $ text='There are $num ducks swimming in a pond' $ echo$text There are $num ducks swimming in a pond $ $ text='There are '$num' ducks swimming in a pond' $ echo$text There are 8 ducks swimming in a pond $ text="There are '$num' ducks swimming in a pond" $ echo$text There are '8' ducks swimming in a pond
tr命令
tr (translate) 命令用于删除或者转换字符,比如大小写转换,删除字符等。命令格式如下:
1
$ tr [OPTION] SET1 [SET2]
OPTION参数说明:
-c | --complement:删除或者替换SET1以外的字符
-d | --delete:删除SET1以外的字符
-s, --squeeze-repeats:压缩连续重复的字符为单个字符
-t, --truncate-set1:截取 SET1 使之与 SET2 长度相等
1. 大小写转换
1 2 3 4 5 6 7 8 9
$ echo HELLO WORLD | tr "A-Z""a-z" hello world $ echo HELLO WORLD | tr "[:upper:]""[:lower:]" hello world $ $ echo hello world | tr "a-z""A-Z" HELLO WORLD $ echo hello world | tr "[:lower:]""[:upper:]" HELLO WORLD
2. 替换
1 2
$ echo"(hello world)" | tr "()""{}" {hello world}
3. 压缩重复字符串
比如可以将多个连续空格压缩为一个空格
1 2
$ echo"hello world !" | tr -s [:space:] ' ' hello world !
echo -e "\033[1m" "There are 8 ducks swimming in a pond" echo -e "\033[0m" "There are 8 ducks swimming in a pond" echo -e "\033[4m" "There are 8 ducks swimming in a pond" echo -e "\033[7m" "There are 8 ducks swimming in a pond" echo -e "\033[32m" "There are 8 ducks swimming in a pond"
文本处理
文件操作
1 2 3 4 5 6 7 8 9 10 11
sed -i '/^$/d' test.txt # 删除空行 sed -i 's/ *//g' test.txt # 删除空格 IFS=$'\n' # linux分隔符,默认是空格 for lines in `cat test.txt`; do # 循环读取每一行 pic=`echo $lines | grep '\!\[\]('` # 处理读取的内容:使用Linux三剑客进行文本处理 if [ "$pic" != "" ] then echo $pic >> new_file.txt fi #do something done