Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LUHN Check in Bash
#!/usr/bin/env bash
. library.sh
pan="$1"
isLUHNValid "$pan"
isLUHNValid() {
local pan="$1"
local panlen="${#pan}"
local sum=0
for ((i = panlen - 1; i >= 0; i--)); do
local digit="${pan:$i:1}"
if (((panlen-i) % 2 == 0)); then
#even
((digit*=2))
((${#digit} == 2)) && digit=$((${digit:0:1}+${digit:1:1}))
fi
((sum+=digit))
done
((sum % 10 == 0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.