removing the trailing slash / on a variable in bash

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.

#!/bin/bash
read -p "Enter Path: " NEWPATH
MYPATH=${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.

#!/bin/bash
read -p "Enter Path: " NEWPATH
MYPATH=${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:

VARIABLE=$1

loosing the trailing slash, written like this:

VARIABLE=${1%/}