简体   繁体   中英

Linux shell string substitution with multiple occurrences

When I use the shell string substitution mechanism, only the first occurrence is replaced.

For instance, If I try to replace the substring @folder with substring mypod in the string:

hostname | grep @folder && cat /etc/hosts | grep @folder

I get

hostname | grep mypod && cat /etc/hosts | grep @folder

Here is what I tried:

root@mypod:/# export var="hostname | grep @folder && cat /etc/hosts | grep @folder"
root@mypod:/# echo $var                                                            
hostname | grep @folder && cat /etc/hosts | grep @folder
root@mypod:/# var2=${var/@folder/mypod}
root@mypod:/# echo $var2
hostname | grep mypod && cat /etc/hosts | grep @folder

What am I missing?

Thanks in advance

${var/@folder/mypod} should be ${var//@folder/mypod}

If you are using bash, here you have a guide on variables expansion you might find useful.

From the bash man-page, section Parameter Expansion : the longest match of pattern against its value is replaced . It does not say "all matches are replaced"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM