Shell string Processing

Source: Internet
Author: User

Shell string Processing

Get string length ${#var}

var="www.baidu.com"echo ${#var}13

String Slice ${var:index:length}, ${var:index} index starting from 0

echo ${var:2}  //从下标为2开始,一直截取到结束w.baidu.comecho ${var:3:8}  //从下标为3开始截取8个字符.baidu.cecho ${var:(-5)}  //截取倒数5个字符u.comecho ${var:(-5):3}   //从倒数第5个字符开始截取3个字符u.c

Replace string ${var/old_str/net_str}, ${var//old_str/new_str}

echo ${var/www/WWW}WWW.baidu.comvar="hello,hello,world,world"echo ${var//hello/HELLO}  //全部替换HELLO,HELLO,world,world

String interception

${var#*delimiter} 以分隔符为临界点(第一个位置),分为左右两段,取右段${var##*delimiter} 以分隔符为临界点(最后一个位置),分为左右两段,取右段${var%%delimiter*} 以分隔符为临界点(最后一个位置),分为左右两段,取左段${var%delimiter*} 以分隔符为临界点(第一个位置),分为左右两段,取左段
[[email protected] ~]# var="http://www.baidu.com//admin/index.php"[[email protected] ~]# echo ${var#*//}www.baidu.com//admin/index.php[[email protected] ~]# echo ${var##*//}admin/index.php[[email protected] ~]# echo ${var%%//*}http:[[email protected] ~]# echo ${var%//*}http://www.baidu.com

Variable state Assignment

${var:-str} //var变量为空返回str${var:+str} //var变量不为空返回str$(var:=str) //var变量为空重新赋值var变量值为str${var:?str} //var变量为空则str输出到stderr

Shell string Processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.