Skip to content

Instantly share code, notes, and snippets.

@jsamuel1
Forked from luciomartinez/slash.sh
Created August 1, 2019 01:54
Show Gist options
  • Save jsamuel1/094d52d79d210aa13cdb860b97a3ec21 to your computer and use it in GitHub Desktop.
Save jsamuel1/094d52d79d210aa13cdb860b97a3ec21 to your computer and use it in GitHub Desktop.
Add or Remove trailing slash in bash
### Add trailing slash if needed
STR="/i/am/a/path"
length=${#STR}
last_char=${STR:length-1:1}
[[ $last_char != "/" ]] && STR="$STR/"; :
echo "$STR" # => /i/am/a/path/
### Remove trailing slash if given
STR="/i/am/a/path/"
length=${#STR}
last_char=${STR:length-1:1}
[[ $last_char == "/" ]] && STR=${STR:0:length-1}; :
echo "$STR" # => /i/am/a/path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment