Cut the path into two parts

Hi,

file=/usr/lib

I need to cut and put it into two variable like

string1=/usr
string2=lib

I made it for string2

string2=${file#/*/}

How to get String1 in the same way which I have get string2.

$ echo ${file%/*}
/usr

Thanks. it is working

 
dirname /usr/lib 

will also work out for you

I suspect your other one is wrong, too - it stops working when the path > two deep.

string1=${file#/*/}

Should be

string1=${file##*/}
$ file=/a/b/c
$ echo ${file#/*/}
b/c
$ echo ${file##*/}
c
1 Like