Skip to content

Commit 15eba7b

Browse files
oliverhenshawljharb
authored andcommitted
[Fix] nvm_strip_path: Preserve leading/trailing colons
Path lists in environmental variables often give special meaning to empty entries (e.g. in PATH or MANPATH). These are represented by leading or trailing colons, or by doubled colons in the middle of the list. Adjust the awk invocation to correctly deal with trailing colons by printing the separator before every field except the first, and then printing the final separator that is read from the input - this will either be a colon or the null string. This preserves leading and trailing colons in all cases while not adding extra colons in the wrong place. Add test to confirm the correct behaviour. Fixes #3144
1 parent a1601ed commit 15eba7b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

nvm.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,8 @@ nvm_strip_path() {
823823
path = substr($0, length(NVM_DIR) + 1)
824824
if (path ~ "^(/versions/[^/]*)?/[^/]*'"${2-}"'.*$") { next }
825825
}
826-
{ print }' | command paste -s -d: -
826+
# The final RT will contain a colon if the input has a trailing colon, or a null string otherwise
827+
{ printf "%s%s", sep, $0; sep=RS } END { printf "%s", RT }'
827828
}
828829

829830
nvm_change_path() {

test/fast/Unit tests/nvm_strip_path

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ TEST_PATH="$NVM_DIR/v0.10.5/bin:/usr/bin:$NVM_DIR/v0.11.5/bin:$NVM_DIR/v0.9.5/bi
1616
STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"`
1717

1818
[ "$STRIPPED_PATH" = "/usr/bin:/usr/local/bin" ] || die "Not correctly stripped: $STRIPPED_PATH "
19+
20+
TEST_PATH=":/a/b/bin::/c/d/bin:"
21+
22+
STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"`
23+
24+
[ "$STRIPPED_PATH" = "$TEST_PATH" ] || die "Stripping does not preserve colons: $STRIPPED_PATH "

0 commit comments

Comments
 (0)