Skip to content

Commit 18a3c10

Browse files
skdas20ljharb
authored andcommitted
[Refactor] read alias files with shell builtins instead of sed/awk/head/tail pipeline
See nvm-sh#3787.
1 parent b4e70cd commit 18a3c10

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
@@ -1323,7 +1323,14 @@ nvm_alias() {
13231323
return 2
13241324
fi
13251325

1326-
command sed 's/#.*//; s/[[:space:]]*$//' "${NVM_ALIAS_PATH}" | command awk 'NF'
1326+
while IFS= read -r line; do
1327+
# Remove comments
1328+
line="${line%%#*}"
1329+
# Trim trailing whitespace
1330+
line="${line%% }"
1331+
line="${line%% }"
1332+
[ -n "${line}" ] && nvm_echo "${line}"
1333+
done < "${NVM_ALIAS_PATH}"
13271334
}
13281335

13291336
nvm_ls_current() {
@@ -1362,7 +1369,16 @@ nvm_resolve_alias() {
13621369
local NVM_ALIAS_INDEX
13631370
NVM_ALIAS_INDEX=1
13641371
while true; do
1365-
ALIAS_TEMP="$( (nvm_alias "${ALIAS}" 2>/dev/null | command head -n "${NVM_ALIAS_INDEX}" | command tail -n 1) || nvm_echo)"
1372+
# Use a more efficient approach with a counter and while loop instead of head/tail
1373+
ALIAS_TEMP=""
1374+
local COUNT=0
1375+
while IFS= read -r line; do
1376+
COUNT=$((COUNT + 1))
1377+
if [ "${COUNT}" -eq "${NVM_ALIAS_INDEX}" ]; then
1378+
ALIAS_TEMP="${line}"
1379+
break
1380+
fi
1381+
done <<< "$(nvm_alias "${ALIAS}" 2>/dev/null || nvm_echo)"
13661382

13671383
if [ -z "${ALIAS_TEMP}" ]; then
13681384
break

0 commit comments

Comments
 (0)