Quantcast
Channel: How to get string between the last two "/" symbols - Super User
Viewing all articles
Browse latest Browse all 5

How to get string between the last two "/" symbols

$
0
0

I am using Bash. I have some string of paths, like /dir0/dir1/dir2/filename.ext and I want to extract the string dir2 in bash. You can assume that the word I want to extract is located between the two last / characters.

I am trying to combine ## and % but with poor results.

What I tried and does not work is cut=${${path%/*}##*/}.

How can I do that?

Solved but looking for alternatives. My solution is the following:

cut=$(tmp=${path%/*}; echo ${tmp##*/})


Viewing all articles
Browse latest Browse all 5

Trending Articles