Say for example I have a path like
path_1=/this/is/a/path/with/slash/
How do I get the following:
/this/is/a/path/with/slash
so the path without the last "/"
Say for example I have a path like
path_1=/this/is/a/path/with/slash/
How do I get the following:
/this/is/a/path/with/slash
so the path without the last "/"
All POSIX shells have (c.f. man bash) "Parameter Expansion: Remove matching suffix pattern". So, use
$ echo "${path_1%/}"
/this/is/a/path/with/slash
If the variable's value does not end in a slash, then the value would be outputted without modification.
You can use realpath
DIR=/tmp/foo///
echo "$(realpath -s "$DIR")"
# output: /tmp/foo
Another way of removing the slash is
$ echo "$(dirname -- "$path")/$(basename -- "$path")"
We just combined the two common shell commands dirname and basename.
You can use sed in a simple way like this:
$ echo $path_1|sed 's-/$--'
/this/is/a/path/with/slash
Explanation:
- the most common is /, but since we are looking for the / itself, it is easier to use some other character. If one really wants to use /, then it would need to be escaped like so:$ echo $path_1|sed 's/\/$//'
There are several options. I usually canonicalise the path. When you canonicalise a path, you get the base path.
For example, if the path is a link to a folder, the canonical form will get the actual path. It will also remove all double-slashes, which although unusual, are allowed in Unix and Linux.
Suppose ~/lf is a link to ~/.hidden/food/limes/.
PATHNAME=~/lf//price/
CANONICAL_PATH="$( realpath --canonicalize-existing "${PATHNAME}" )"
echo "${CANONICAL_PATH}"
The result would be /home/kamil/.hidden/food/limes/price
This also works for files, block devices, etc., although of course they don't have a trailing slash. For example, on my system:
PATHNAME=/dev/disk/by-partlabel/Boot
CANONICAL_PATH="$( realpath --canonicalize-existing "${PATHNAME}" )"
echo "${CANONICAL_PATH}"
The result is /dev/nvme0n1p2
If you aren't sure that the path exists, you need to add some error-checking.
PATHNAME=~/lf//price/
CANONICAL_PATH="$( realpath --canonicalize-existing "${PATHNAME}" 2>/dev/null )"
if [[ -z ${CANONICAL_PATH} ]]
then
echo "Path doesn't exist: ${PATHNAME}" >&2
else
echo "Canonical path is ${CANONICAL_PATH}"
fi