Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Created October 15, 2020 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourdollars/af5c990874148915290046108afdc393 to your computer and use it in GitHub Desktop.
Save fourdollars/af5c990874148915290046108afdc393 to your computer and use it in GitHub Desktop.
A Bash script to strip last N elements
#!/bin/bash
strip_last_n()
{
n="$1"
shift
if [ "$n" -ge "$#" ]; then
return
else
len="$(($# - n))"
echo "${@:1:$len}"
fi
}
array=(0 1 2 3 4 5 6 7 8 9)
for i in {1..10}; do
echo "strip last $i"
strip_last_n "$i" "${array[@]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment