Created
April 5, 2016 13:55
-
-
Save jmmitchell/a6eacafadefb5292c9abdadc8bab7852 to your computer and use it in GitHub Desktop.
One liner for adding or removing trailing slash of a string in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Add a trailing slash to a string if there is not one already | |
STR="/some/example/path-without-slash" | |
echo "${STR}$(printf \\$(printf '%03o' $(($(printf '%d' "'${STR:(-1)}")==47?0:47))))" | |
# remove a (single) trailing slash on a string if there is one | |
STR="/some/example/path-with-slash/" | |
echo "${STR%/}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment