need sed command to read a path and set to variable

I have a variable called PATH that contains a path
example: /Users/rtipton/Desktop/testusers/test

I need a sed command to set a variable called USER to the last directory name in that path

PATH="/Users/rtipton/Desktop/testusers/test"

and from that PATH i need USER to = test

I know sed can do this but sed usage is still a bit of a mystery to me :slight_smile:

Thanks,
Rob

# USER=`echo "/Users/rtipton/Desktop/testusers/test" | sed 's/.*\/\(.*\)/\1/'`;echo $USER
test

Very nice, works well. Can you do me a huge favor and break down the sed command you used here and why it works the way it does... I am still trying to figure out how to properly use it and I learn by examples better than reading hypothetical explanations.

Thanks,
Rob

's/ --- Substitute directive
./ --- contains everything upto last / ("/Users/rtipton/Desktop/testusers/")
(.
) --- contains everything after last / (test)
/\1/ --- what to substitute (like $1 variable, what is inside the ( ) is stored in \1 )

there are extra \ because you have to escape metacharacters

I didnt know much about sed and awk before I found this site. The way I am learning, is people ask how to do something, I read others solutions and try to replicate it.

I have created my own wiki to keep all my notes in.

here is one site I found helpful:

http://student.northpark.edu/pemente/sed/sed1line.txt

Must it be sed??

MYPATH="/Users/rtipton/Desktop/testusers/test"
USER=`basename $MYPATH`