Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active May 28, 2023 00:33
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 coolaj86/682bd23d936094998be1b1c259da0269 to your computer and use it in GitHub Desktop.
Save coolaj86/682bd23d936094998be1b1c259da0269 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
set -u
fn_replace_home() {
my_path="${1}"
my_delim="$(
printf '\v'
)"
my_path="$(
echo "${my_path}" |
sed "s${my_delim}${HOME}${my_delim}\$HOME${my_delim}g" |
sed "s${my_delim}\${HOME}${my_delim}\$HOME${my_delim}g" |
sed "s${my_delim}^~${my_delim}\$HOME${my_delim}g"
)"
echo "${my_path}"
}
fn_replace_home /usr/bin
fn_replace_home ~/.local/bin
fn_replace_home "${HOME}/.local/xbin"

Expected Output

/usr/bin
$HOME/.local/bin
$HOME/.local/a/bin
$HOME/.local/b/bin
$HOME/.local/c/bin
$HOME/.local/d/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment