Well there are probably many ways and I have seen a few, let us keep it simple and use built in stuff as much as possible.

Bash is for lazy people.

read -p "Enter Path: " NEWPATH
echo ${MYPATH}

Using this, the path can have a trailing slash and you have got it stuck in the variable. Or it could not have one. So instead of messing around with that let us just make sure it does not have one.

read -p "Enter Path: " NEWPATH
echo ${MYPATH}

Here we are removing the trailing slash if there is one. All it takes is the “%/”.

Reference: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Simpler shown on grabbing input on your script. Normally written like this:


loosing the trailing slash, written like this:



removing the trailing slash / on a variable in bash
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

The CAPTCHA cannot be displayed. This may be a configuration or server problem. You may not be able to continue. Please visit our status page for more information or to contact us.