Variable substring

1.Calculate the length of the variable

    [root@dong ~]# CHAR="I am dong"
    [root@dong ~]# echo $CHAR
    I am dong
    [root@dong ~]# echo ${#CHAR}
    9
    [root@dong ~]# echo $CHAR|wc -L
    9

2.Number of character from the second to the end(including spaces)

    [root@dong ~]# echo ${CHAR:2}
    am dong

3.Number of character from the second character to the N character(including spaces)
[root@dong ~]# echo ${CHAR:2:2}
am
[root@dong ~]# echo ${CHAR:2:4}
am d
[root@dong ~]# echo $CHAR|cut -c 3-9
am dong

4.匹配变量内容替换

    [root@dong ~]# CHAR="I am dong dong I am dong"
    [root@dong ~]# echo ${CHAR/dong/ling}
    I am ling dong I am dong
    Suffix Matching
    [root@dong ~]# echo ${CHAR/%dong/ling}
    I am dong dong I am ling
    [root@dong ~]# echo $CHAR
    abcABC123ABCabc
    [root@dong ~]# echo ${CHAR#a*C}
    123ABCabc
    [root@dong ~]# echo ${CHAR##a*C}
    abc
    [root@dong ~]# echo ${CHAR%%a*c}

    [root@dong ~]# echo ${CHAR%a*c} 
    abcABC123ABC

Because variable subtrings are rarely used,BAIDU can be used if necessary

Use Skills
rename file
echo $file
adfa.jpg
mv $file ${file%jpg/png}

转载于:https://blog.51cto.com/6207422/2411175

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值