Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash - safely rm from a script
under_home() {
# $HOME has to be set. Should always be, but make sure.
[[ -z "$HOME" ]] && return 1
# Handle tricky trailing slash inputs. ie: $HOME/// becomes $HOME
local somepath="$( echo "$1" | tr -s '/' )"
somepath=${somepath%/}
# Tailing slashes already removed, so now ensure the path lives under $HOME
[[ "$somepath" == "$HOME/"* ]] && return 0 || return 1
}
rmsafely() {
under_home "$1" || return 1
command rm -rf "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.