Skip to content

Instantly share code, notes, and snippets.

@c0des1ayr
Last active July 13, 2025 03:46
Show Gist options
  • Select an option

  • Save c0des1ayr/3f34ea49acf1e1c34d931d4ffb1afaf6 to your computer and use it in GitHub Desktop.

Select an option

Save c0des1ayr/3f34ea49acf1e1c34d931d4ffb1afaf6 to your computer and use it in GitHub Desktop.
Bash <4 mapfile implementation (works down to Bash 2.05b)
filemap(){ # custom back-compat implementation of mapfile bc why not
STRIP="f"
ARRAY_NAME=""
[[ $1 = "-t" ]] && STRIP="t" || ARRAY_NAME=$1
[[ $ARRAY_NAME = "" ]] && ARRAY_NAME=$2
LINENUM=0
for((;;)){
read -r || break
[[ $STRIP = "t" ]] && printf -v "$ARRAY_NAME[$LINENUM]" '%s' "${REPLY%"${_##*[!\\\n]}"}" || printf -v "$ARRAY_NAME[$LINENUM]" '%s' "$REPLY"
((LINENUM++))
}
}
#usage: like mapfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment