Skip to content

Instantly share code, notes, and snippets.

@pille1842
Last active June 2, 2022 21:33
Show Gist options
  • Select an option

  • Save pille1842/9667dad2f860d861d023f71c1ac22edc to your computer and use it in GitHub Desktop.

Select an option

Save pille1842/9667dad2f860d861d023f71c1ac22edc to your computer and use it in GitHub Desktop.
Validate IBANs in the shell
published: true
#!/bin/bash
# Script to validate IBAN (International Bank Account Numbers)
# Usage: iban.sh IBAN
# Return value: 0 if valid, 1 if invalid, 2 if bc not found
# Get the ASCII numeric value of a letter
ord() {
LC_CTYPE=C printf '%d' "'$1"
}
# Get the position of a letter in the alphabet + 9, or the ASCII value - 55.
# We need this to validate the checksum.
alphabet_pos() {
echo $((`ord $1` - 55))
}
# Check for bc
BC=$(which bc)
if [[ ! -x "$BC" ]]; then
echo "Error: no executable bc could be found. This script relies on bc being in your PATH."
exit 2
fi
# First, let's remove all blanks from the IBAN
IBAN=${1//[[:blank:]]/}
# Then extract some parts of it:
# - the two-letter country
# - the two-digit checksum
# - the actual account number (BBAN)
COUNTRY=${IBAN:0:2}
CHECKSUM=${IBAN:2:2}
BBAN=${IBAN:4}
# Then we replace the country's letters with their position in the alphabet + 9.
COUNTRYSUM=`alphabet_pos ${COUNTRY:0:1}``alphabet_pos ${COUNTRY:1}`
# Finally, let's combine BBAN, numerical country and checksum.
OPERAND=`echo $BBAN``echo $COUNTRYSUM``echo $CHECKSUM`
# Then we need to check that this number mod 97 equals 1. If so, the IBAN is valid.
# Since the number is very large, we use the arbitrary precision calculator bc(1)
# for this purpose.
if [[ `echo "$OPERAND % 97" | $BC` -eq 1 ]]; then
echo -e "IBAN \e[32mvalid"
exit 0
else
echo -e "IBAN \e[31minvalid"
exit 1
fi

My fraternity is preparing for its big 175-year celebrations, and we have published the festival program, which includes our festival bank account number. Of course this number needed to be validated. A lot. Because you do not want to publish this thing with an incorrect bank account number.

Since the number is an IBAN, or International Bank Account Number, it actually includes a two-digit checksum that can be used to validate it. There are a lot of websites out there, probably including your own bank, that offer to validate IBANs. But I don't like having to visit a website everytime I want to check an account number, giving the website's owner access to that number and using the Internet for some easy mathematical calculations that my computer can do completely on its own.

Wikipedia actually lists the procedure required to validate an IBAN. (I used the German-language Wiki article, though.) Short answer is: Bite off the two-letter country ID and the checksum; replace each letter in the country ID with its position in the alphabet plus nine; then put together the actual account number + the numerical value of the country ID + the checksum, and see if that mod 97 is one. If it is, the IBAN is valid. If it isn't, well... duh.

So after reading up on doing some basic string manipulation in Bash, I came up with this little script that does the job for you. No internet connection required. Just give it your IBAN as argument one and it will print out a nice little colored message.

Just click "View on GitHub" down there to see the script file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment