Skip to content

Commit fccd2ca

Browse files
committed
refactor: read alias files with shell builtins instead of sed/awk/head/tail pipeline (fixes nvm-sh#3787)
1 parent 4c556a1 commit fccd2ca

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

nvm.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,14 @@ nvm_alias() {
13141314
return 2
13151315
fi
13161316

1317-
command sed 's/#.*//; s/[[:space:]]*$//' "${NVM_ALIAS_PATH}" | command awk 'NF'
1317+
while IFS= read -r line; do
1318+
# Remove comments
1319+
line="${line%%#*}"
1320+
# Trim trailing whitespace
1321+
line="${line%% }"
1322+
line="${line%% }"
1323+
[ -n "${line}" ] && nvm_echo "${line}"
1324+
done < "${NVM_ALIAS_PATH}"
13181325
}
13191326

13201327
nvm_ls_current() {
@@ -1353,7 +1360,16 @@ nvm_resolve_alias() {
13531360
local NVM_ALIAS_INDEX
13541361
NVM_ALIAS_INDEX=1
13551362
while true; do
1356-
ALIAS_TEMP="$( (nvm_alias "${ALIAS}" 2>/dev/null | command head -n "${NVM_ALIAS_INDEX}" | command tail -n 1) || nvm_echo)"
1363+
# Use a more efficient approach with a counter and while loop instead of head/tail
1364+
ALIAS_TEMP=""
1365+
local COUNT=0
1366+
while IFS= read -r line; do
1367+
COUNT=$((COUNT + 1))
1368+
if [ "${COUNT}" -eq "${NVM_ALIAS_INDEX}" ]; then
1369+
ALIAS_TEMP="${line}"
1370+
break
1371+
fi
1372+
done <<< "$(nvm_alias "${ALIAS}" 2>/dev/null || nvm_echo)"
13571373

13581374
if [ -z "${ALIAS_TEMP}" ]; then
13591375
break

0 commit comments

Comments
 (0)