Quote:
Originally Posted by FreddyMro
Hi,
this is my code for /bin/bash:
function removeFirstSubstring {
tempVar=${1/$2/""}
echo $tempVar
}
var="one -two three -four five"
var=$(removeFirstSubstring "$var" "-two * ")
echo $var
I want to have the output "one -four five". At the moment it only gives me "one five" though.
Can you help me?
Thank you in advance!
|
Your "-two *" is expanding to -two followed by a list of everything in the current folder. The results in a user function or bash internal function will depend upon what is in the folder, which is not optimal in this case. If you replace
with
you may get the desired output.
You do not say, but I infer that you are attempting to get a match to two strings, one fixed and one regex, in the same parameter. Can you confirm or contradict this inference?
Getting something CLOSE to what I did infer would be using single quotes instead of double quotes to delimit that parameter. In this case you would NOT get what you want, since the * would match ALL the remaining string rather than just the next word. Think about that a bit, and you may see a few possible solutions. (None of which will matter if my inference is false.)
I look forward to seeing your reply.