Bash - String replace at the end with a regex
Hi,
I want to remove the last foldername in a string variable: "hello/mr/robinson/" should become "hello/mr/" I am trying it the following way: a="hello/mr/robinson/" echo ${a//[^\/]*\/$/} But it seems that ${} does not recognize $ as end of string delimiter. Any ideas? |
Hello, how about
dirname $a Cheers, Tom. |
Quote:
|
I also just noticed that with dirname I will get a ".", if I have "hello/" for example. But I want that to become ""
|
Quote:
try this: Code:
echo ${a##${a%%*([^/])/}} A few more notes: The part in bold is called an extended pattern. You might have to set it in bash before you can use it: Code:
shopt -s extglob Hope this helps. |
Quote:
Code:
echo ${a%%*([^/])/} |
Oops
Quote:
|
parameter expansions don't use regular expressions, they follow shell globbing rules.
Don't forget that you can also add characters back to the string after expansion, which may help simplify things in some places. To retain the following backslash on "/hello/mr/", for example, you could simply do this: Code:
a="hello/mr/robinson/" |
All times are GMT -5. The time now is 11:33 PM. |