Skip to content

Instantly share code, notes, and snippets.

@cth
Created March 12, 2014 11:43
Show Gist options
  • Save cth/9505264 to your computer and use it in GitHub Desktop.
Save cth/9505264 to your computer and use it in GitHub Desktop.
Select index of a particular named argument
function nth() {
target=$1
shift
idx=1
for symbol in $@; do
if [ $symbol == $target ]; then
echo $idx
return
fi
idx=$(($idx+1))
done
}
# Example:
# nth b a b c
# -> 2
#
# Useful for, e.g., cut
# cut -f $(nth fieldofinterest $(head f.txt)) f.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment