Shell – how to strip the last slash of the directory path

filenamesshell-scriptstring

I have a script which requires an directory as one argument.
I want to support the two form: one is like

a/b/c

(no slash at the end) and another is like

 a/b/c/

(has slash at the end).

My question: given either of the two form, how can I just keep the first form unchanged and strip the last slash of the second form to convert it to the first form.

Best Answer

dir=${1%/}

will take the script's first parameter and remove a trailing slash if there is one.

Related Question