Skip to content

Instantly share code, notes, and snippets.

@jmmitchell
Created April 5, 2016 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmmitchell/a6eacafadefb5292c9abdadc8bab7852 to your computer and use it in GitHub Desktop.
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
#!/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