How-To Trim user input

Hi,

The user inputs either "/tmp/bea" or "/tmp/bea/". Regardless my script should extract and store in a variable the last directory in the user input path.

Desired output: "bea"

Please help.

var=$(basename /tmp/bea/)
1 Like

May be you can try parameter expansion.

Shell Command Language

Does basename command work across all platforms ?

When the user input is "/tmp"

I get the below error

basename <directorywhich has complete path>
 
eg:(solaris 9)
$ basename /tmp
tmp
 

It worked OK for me:

$ basename /tmp
tmp

Which unix version are you in?