+#!/bin/bash
+
+prune_path_rootside() {
+ # Desc: Prunes a path from the root-side to a specified prune level.
+ # Input: arg1 str path
+ # arg2 int prune level (0-indexed)
+ # Depends: GNU sed 4.8
+ # Version: 0.0.1
+ local path="$1";
+ local prune_level="$2";
+
+ # Check for absolute or relative path
+ if [[ "$path" =~ ^/ ]]; then
+ flag_root=true;
+ # Remove initial /
+ path="$(echo "$path" | sed -e 's:^/::' )";
+ else
+ flag_root=false;
+ fi;
+
+ # Save path as array with `/` as element delimiter
+ local IFS='/';
+ read -ra parts <<< "$path";
+
+ # Assemble pruned path from prune_level
+ local pruned_path="";
+ for (( i=prune_level; i<${#parts[@]}; i++ )); do
+ pruned_path+="${parts[i]}/";
+ done;
+
+ # Trim trailing `/` delimiter
+ pruned_path=$(echo "$pruned_path" | sed 's:/*$::');
+
+ # Restore initial / if appropriate
+ if [[ "$flag_root" == "true" ]] && [[ "$prune_level" -eq 0 ]]; then
+ pruned_path=/"$pruned_path";
+ fi;
+
+ # Output pruned path
+ echo "$pruned_path";
+ #declare -p path prune_level parts pruned_path && printf "========\n"; # debug
+ return 0;
+}; # prune path rootside to int specified level
+
+printf "========Test 1========\n";
+prune_path_rootside "foo/bar/baz" 0;
+prune_path_rootside "foo/bar/baz" 1;
+prune_path_rootside "foo/bar/baz" 2;
+prune_path_rootside "foo/bar/baz" 3;
+printf "========Test 2========\n";
+prune_path_rootside "/foo/bar/baz" 0;
+prune_path_rootside "/foo/bar/baz" 1;
+prune_path_rootside "/foo/bar/baz" 2;
+prune_path_rootside "/foo/bar/baz" 3;