1、内置命令 [root@node01 ~]# which cd /usr/bin/which: no cd in (/data/apache-maven-3.3.9/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) [root@node01 ~]# man cd BASH_BUILTINS(1) BASH_BUILTINS(1) NAME bash,:,.,[,alias,bg,bind,break,builtin,caller,cd,command,compgen,complete,compopt,continue,declare,dirs,disown,echo,enable,eval,exec,exit,export,false,fc,fg,getopts,hash,help,history,jobs,kill,let,local,
logout,mapfile,popd,printf,pushd,pwd,read,readonly,return,set,shift,shopt,source,suspend,test,times,trap,true,type,typeset,ulimit,umask,unalias,unset,wait - bash built- in commands,see bash(1) 2、变量子串常用操作(了解) man bash找本节资料 ${#string} 打印$string的长度 ${#string:position} 在$string中,从位置%position之后开始
提取字串 ${#string:position:length} 在$string中,从位置%position之后开始
提取长度为$length的字串 例子: [root@node01 ~]# WOLF="hello yujing" [root@node01 ~]# echo $WOLF hello yujing [root@node01 ~]# echo ${#WOLF} 和wc -L 相同 12 [root@node01 ~]# echo $WOLF |wc -L 12 root@node01 ~]# echo ${WOLF:2} llo yujing [root@node01 ~]# echo ${WOLF:2:2} ll [root@node01 ~]# echo ${WOLF:2:4} 和cut -c相同 llo [root@node01 ~]# echo $WOLF |cut -c 3-4 ll [root@node01 ~]# echo $WOLF |cut -c 3-6 llo
删除 ${#string#substring} 从变量$string开头开始
删除最短匹配$substring子串 ${#string##substring} 从变量$string开头开始
删除最长匹配$substring子串 ${#string%substring} 从变量$string结尾开始
删除最短匹配$substring子串 ${#string%%substring} 从变量$string结尾开始
删除最长匹配$substring子串 替换 ${#string/substring/replace} 使用$replace,来代替第一个匹配的$substring ${#string/#substring/replace} 如果$string前缀匹配$substring,用$replace来代替匹配substring ${#string/%substring/replace} 如果$string后缀匹配$substring,用$replace来代替匹配substring 例子: [root@node01 ~]# WOLF="hello yujing" hello yujing [root@node01 ~]# echo ${WOLF/yujing/laolang} hello laolang 实例: 1、通过变量子串替换 vi laolang.log wolf_20170806_1_wolf.jpg wolf_20170806_2_wolf.jpg wolf_20170806_3_wolf.jpg wolf_20170806_4_wolf.jpg wolf_20170806_5_wolf.jpg wolf_20170806_6_wolf.jpg wolf_20170806_7_wolf.jpg wolf_20170806_8_wolf.jpg wolf_20170806_9_wolf.jpg wolf_20170806_10_wolf.jpg [root@node01 data]# touch `cat laolang.log |tr "\n" " "` [root@node01 data]# ll total 8316 drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9 -rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz -rw-r--r-- 1 root root 0 Jul 29 14:16 cat drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1 drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2 drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3 -rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_wolf.jpg [root@node01 data]# f=wolf_20170806_10_wolf.jpg [root@node01 data]# echo $f wolf_20170806_10_wolf.jpg [root@node01 data]# echo ${f%wolf*.jpg} wolf_20170806_10_ [root@node01 data]# mv $f `echo ${f%wolf*.jpg}.jpg` [root@node01 data]# ll total 8316 drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9 -rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz -rw-r--r-- 1 root root 0 Jul 29 14:16 cat drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1 drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2 drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3 -rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_wolf.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_wolf.jpg [root@node01 data]# for f in `ls *wolf.jpg` ;do mv $f `echo ${f%wolf*.jpg}.jpg`;done [root@node01 data]# ll total 8316 drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9 -rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz -rw-r--r-- 1 root root 0 Jul 29 14:16 cat drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1 drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2 drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3 -rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.jpg 2、
修改扩展名 把上面的jpg更换为log [root@node01 data]# f=wolf_20170806_10_.jpg [root@node01 data]# echo ${f/%jpg/log} wolf_20170806_10_.log [root@node01 data]# mv $f ${f/%jpg/log} [root@node01 data]# ls apache-maven-3.3.9 day1 laolang.log wolf_20170806_2_.jpg wolf_20170806_5_.jpg wolf_20170806_8_.jpg apache-maven-3.3.9-bin.tar.gz day2 wolf_20170806_10_.log wolf_20170806_3_.jpg wolf_20170806_6_.jpg wolf_20170806_9_.jpg cat day3 wolf_20170806_1_.jpg wolf_20170806_4_.jpg wolf_20170806_7_.jpg [root@node01 data]# ll total 8316 drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9 -rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz -rw-r--r-- 1 root root 0 Jul 29 14:16 cat drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1 drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2 drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3 -rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.jpg -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.jpg [root@node01 data]# for f in `ls *.jpg` ;do mv $f ${f/%jpg/log} ;done [root@node01 data]# ll total 8316 drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9 -rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz -rw-r--r-- 1 root root 0 Jul 29 14:16 cat drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1 drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2 drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3 -rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.log -rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.log ====================================================================== 网上资料 http://blog.csdn.net/github_33736971/article/details/53980123 ${#string} 返回$string的长度 ${string:position} 在$string中,从$position位置之后开始
提取子串 ${string:position:length} 在$string中,从$position位置之后开始
提取$length长度的子串 [root@localhost SHELL]# NAME="ni hao,ming tian" [root@localhost SHELL]# echo $NAME ni hao,ming tian [root@localhost SHELL]# echo ${NAME} ni hao,ming tian [root@localhost SHELL]# echo ${#NAME} 17 [root@localhost SHELL]# echo ${NAME:3} hao,ming tian [root@localhost SHELL]# echo ${NAME:3:6} hao,m [root@localhost SHELL]# ${string#substring} 从变量$string开头开始
删除最短匹配$substring子串 ${string##substring} 从变量$string开头开始
删除最长匹配$sunstring子串 [root@localhost SHELL]# echo $NAME abcABC123ABCabcde [root@localhost SHELL]# echo ${NAME#a*C} 123ABCabcde [root@localhost SHELL]# echo ${NAME##a*C} abcde [root@localhost SHELL]# ${string%substring} 从变量$string结尾开始
删除最短匹配$substring子串 ${string%%substring} 从变量$string结尾开始
删除最长匹配$substring子串 [root@localhost SHELL]# NAME=abcABC123ABCabc [root@localhost SHELL]# echo $NAME abcABC123ABCabc [root@localhost SHELL]# echo ${NAME%%a*c} [root@localhost SHELL]# echo ${NAME%a*c} abcABC123ABC [root@localhost SHELL]# echo ${NAME#b*c} abcABC123ABCabc [root@localhost SHELL]# echo ${NAME#bc} abcABC123ABCabc [root@localhost SHELL]# echo ${NAME%bc} abcABC123ABCa [root@localhost SHELL]# echo ${NAME%%bc} abcABC123ABCa [root@localhost SHELL]# echo ${NAME%C*c} abcABC123AB [root@localhost SHELL]# echo ${NAME%%C*c} abcAB [root@localhost SHELL]# echo ${NAME%%C*b} abcABC123ABCabc [root@localhost SHELL]# echo ${NAME} abcABC123ABCabc [root@localhost SHELL]# 注意:在进行#或##匹配时,$string的首字符必须是被
删除子串$substring的第一个字符,不然无法匹配
删除; 在进行%或者%%匹配时,$string的最后一个字符必须是被
删除子串$substring的最后一个字符,不然无法进行
删除操作; ${parameter/parttern/string} 用string来替换第一个匹配的pattern ${parameter/#parttern/string} 从开头匹配parameter变量中的pattern,匹配上后就用string来替换匹配的pattern ${parameter/%pattern/string} 从结尾开始匹配parameter变量中的pattern,匹配上后就用string替换匹配的pattern ${parameter//pattern/string} 用string来替换parameter变量中所有匹配的pattern [root@localhost SHELL]# NAME="I am a student student" [root@localhost SHELL]# echo ${NAME//student/teacher} I am a teacher teacher [root@localhost SHELL]# echo ${NAME/#student/teacher} I am a student student [root@localhost SHELL]# echo ${NAME/%student/teacher} I am a student teacher [root@localhost SHELL]# echo ${NAME/%I am /teacher} I am a student student [root@localhost SHELL]# echo ${NAME/#I am /teacher} teachera student student [root@localhost SHELL]# echo ${NAME/#I am a/teacher} teacher student student [root@localhost SHELL]# echo $NAME I am a student student [root@localhost SHELL]# ===========================================================================
原文链接:https://www.f2er.com/bash/391590.html