Skip to content

Instantly share code, notes, and snippets.

@if1live
Last active August 29, 2015 14:14
Show Gist options
  • Save if1live/b23494b9e42ae89e6f28 to your computer and use it in GitHub Desktop.
Save if1live/b23494b9e42ae89e6f28 to your computer and use it in GitHub Desktop.
Factorial (shell script edition). Shell script is a function
#!/bin/bash
if [ -z "$1" ] || [ "1" == "$1" ]; then
echo 1
else
next=$(( $1 - 1 ))
prev=$(./factorial.sh $next)
echo $(( $prev * $1 ))
fi
# $ ./factorial.sh 1
# 1
# $ ./factorial.sh 2
# 2
# $ ./factorial.sh 3
# 6
# $ ./factorial.sh 4
# 24
# $ ./factorial.sh 5
# 120
# $ ./factorial.sh 6
# 720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment