Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Linux delete the value of a replacement variable

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章将为大家详细讲解有关Linux怎么删除替换变量值,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

在Linux系统中,对变量进行赋值后需要发现有些问题,需要对变量值进行删除或替换,本文将针对Linux变量值的替换和删除做个详细介绍

1、变量内容的删除

[root@bogon ~]# echo ${PATH} #呼叫出PATH变量的值

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin

[root@bogon ~]# echo ${PATH#/*bin:} #看见那个"#"符号,表示从变量"PATH"值的左边开始向右边删除能匹配到"*bin:"的位置,仅删除能匹配到最短的那个,所以输出内容如下,"/usr/local/sbin:"被删除了

/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin

[root@bogon ~]# echo ${PATH#*local/bin:}

/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin

[root@bogon ~]# echo ${PATH##/*bin:} #两个"#"表示删除能匹配到最长的那个,所以"/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:"全部被删除

/root/bin

[root@bogon ~]# echo ${PATH%:/*bin} #把上边的"#"号换成"%"看看,表示从变量"PATH"值的右边开始向左边删除能匹配到"*bin"的位置,仅删除能匹配到最短的那个,所以":/root/bin"被删除

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin

[root@bogon ~]# echo ${PATH%%:/*bin} #两个"%"与两个"#"类似的道理

/usr/local/sbin

2、变量内容的替换

[root@bogon ~]# echo ${PATH/sbin/SBIN/} #用"SBIN"替换"sbin",但只替换第一次匹配到的

/usr/local/SBIN/:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin

[root@bogon ~]# echo ${PATH//sbin/SBIN/} #用"SBIN"替换"sbin",全局替换

/usr/local/SBIN/:/usr/local/bin:/SBIN/:/bin:/usr/SBIN/:/usr/bin:/usr/local/mysql/bin:/root/bin

变量内容删除与替换小结:

变量书写方式功能描述

${变量名称#匹配字符}若变量内容有被匹配到,刚从变量内容的左边开删除,但删除匹配最短的

${变量名称##匹配字符}若变量内容有被匹配到,刚从变量内容的左边开删除,但删除匹配最长的,可理解成此时工作在贪婪模式

${变量名称%匹配字符}若变量内容有被匹配到,刚从变量内容的右边开删除,但删除匹配最短的

${变量名称%%匹配字符}若变量内容有被匹配到,刚从变量内容的右边开删除,但删除匹配最长的,可理解成此时工作在贪婪模式

${变量称/旧字符串/新字符串}从变量内容的左边开始匹配,用新字符串替换旧字符串,仅替换第一个匹配到的字符串

${变量称//旧字符串/新字符串}从变量内容的左边开始匹配,用新字符串替换旧字符串,全部替换匹配到的字符串

关于"Linux怎么删除替换变量值"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report