fibonacci() {
    local n=$1
    local a=0
    local b=1
    local i=2

    if [ $n -eq 0 ]; then
        echo 0
        return
    elif [ $n -eq 1 ]; then
        echo 1
        return
    fi

    while [ $i -le $n ]; do
        local temp=$((a + b))
        a=$b
        b=$temp
        i=$((i + 1))
    done

    echo $b
}

# Main script
echo "This script calculates the Nth Fibonacci number."
read -p "Enter the value of N: " num

# Call the function to compute the Nth Fibonacci number
result=$(fibonacci $num)
echo "The $num-th Fibonacci number is: $result" 

Online Bash Shell

Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.

About Bash

Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.

Features

  • Open source GNU project
  • Read and execute the commands from a Shell Script
  • Can be invoked by either single-character command line options (-a, -b, -c, -i, -l, -r, etc. ) or by multi-character command line options also like --help, --debugger,--login, etc.
  • Consists of Key bindings
  • Available in restricted mode for the environment security
  • Contains one-dimensional arrays to manipulate the lists of data.

Syntax help

Variables

name="Foo"
echo $name
echo "$name"
echo "${name}"

Conditional Statements

If

if [ conditional-expression ]  
then  
statements  
fi  

If-else

if [ conditional-expression ]  
then  
   statements  
else  
  statements
fi  

Else-If

if [ conditional-expression ]  
then  
   statements  
elif [ conditional-expression ]  
then  
 statements  
else  
  statements
fi