Linux how to delete the replacement variable value

  

In the Linux system, you need to find some problems after assigning variables, you need to delete or replace the variable values. This article will give a detailed introduction to the replacement and deletion of Linux variable values. It is helpful to you.

1, the content to delete variable

[root @ bogon ~] # echo $ {PATH} # PATH variable value of the call

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

[root@bogon ~ ]# echo ${PATH#/*bin:} #See the ""#” symbol, indicating that the position from the left side of the variable "PATH" is deleted to the right and can be matched to the position of "*bin:”" The deletion can match the shortest one, so the output is as follows, “/usr/local/sbin:” is deleted

/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 :} #两“#” indicates that the deletion can match the longest one, so “/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local /mysql/bin:”all deleted

/ro Ot/bin

[root@bogon ~]# echo ${PATH%:/*bin} #Change the above “#” number to “%” look, from the variable &ldquo The right side of the value of PATH” starts to delete to the left and can match the position of “*bin”, only the one that matches the shortest is deleted, so “:/root/bin” is deleted

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

[root@bogon ~]# echo $ {PATH%%:/*bin} #两“%” Similar to the two “#”

/usr/local/sbin

2, variable content Replacement

[root@bogon ~]# echo ${PATH/sbin/SBIN/} #Replace “sbin” with “SBIN”, but only replace the first matching

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

[root@bogon ~]# echo ${PATH//sbin/SBIN/} #Replace “sbin”, globally replace

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

Variable content deletion and replacement summary:

Variable writing mode function description

${variable name#matching character} If the variable content is matched, just delete it from the left side of the variable content, but delete the shortest matching

${variable name##matching character} If the variable content is matched, just delete it from the left side of the variable content, but delete the longest match, which can be understood as working in greedy mode

$ {variable name% match character} If the variable content is matched, just delete from the right side of the variable content, but delete the shortest matching

${variable name%% matching character} if the variable content is matched To, just delete from the right side of the variable content, but delete the longest match, can be understood as working in greedy mode at this time

${variable scale/old string/new string} from the variable content Matches on the left, replaces the old string with a new string, and replaces only the first matched string

${variable scale //String/new string} matches from the left side of the variable content, replaces the old string with the new string, and replaces the matched string with all

The above is the method for Linux to delete and replace variable values. This article only introduces the two methods of deleting and replacing, the modification of variables and the replacement methods, etc., will not be introduced here.

Copyright © Windows knowledge All Rights Reserved