I have a string variable $LIBRARIES="abc.so.1 def.so.1 hij.so.3.1" and I want to replace all the .so such that they look like this:
"abc.so* def.so* hij.so*"
How can I do this? I tried NEW_LIBRARIES=${LIBRARIES//.so*/.so$star} but it doesn’t work. How can I tell it to end on whitespace?
This should do it:
Output:
Explanation:
LIBRARIES="...": When assigning a string to a variable, the variable is not prefixed with$NEW_ALL_LIBRARIES=$(...): The$(...)notation is called command subsitution; basically it spawns a new subshell to run whatever commands contained within, then returns the output to this new subshell’sstdout(and here saving it toNEW_ALL_LIBRARIES).sed: invoke sed, the Streaming EDitor tool's/\.so[^ ]*/\.so\*/g': Use regular expressions (regex) to match patterns and substitute. Let’s break this syntax down a bit further:s/“substitute”; For example:s/A/B/greplaces all occurrences ofAwithB\.so[^ ]*/: Match any patterns that start with.so, and the[^ ]*part means “followed by zero or more non-space characters”\.so\*/: Replace that with literally.so*[,],.and*have special meaning in regex, so if you mean to use them literally, you just “escape” them by prefixing a\g: Do so for all occurrences, not just the first.<<< "$LIBRARIES": the<<<notation is called herestring: in this context it accomplishes the same thing asecho "$LIBRARIES" | sed ..., but it saves a subshell.