Showing posts with label Calculating factorial using bash one liner. Show all posts
Showing posts with label Calculating factorial using bash one liner. Show all posts

Tuesday, December 6, 2022

Calculating factorial using bash one liner

 

I was trying to get the factorial of a number in bash,it's an easy program obviously ,using loop I can do this,for example

#!/usr/bin/bash
echo "Enter number"
read number

result=1
for((i=1;i<=number;i++))
        do
                result=$(($result*i))
        done
echo "The factorial of $number is $result"

Then I tried to find some one liners,I mean bash is famous for one liners ,using seq and bc it worked just fine.

sourav@LAPTOP-HDM6QEG8:~$ num=5
sourav@LAPTOP-HDM6QEG8:~$ seq -s "*" 1 $num | bc
120

Using $(( it also worked like it should

sourav@LAPTOP-HDM6QEG8:~$ echo $((`seq -s "*" 1 $num`))
120

However when I am trying to use expr I am not able to do it.

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " * " 10`
expr: syntax error: unexpected argument ‘0’

I thought since * is an universal symbol may be I should escape it,but it still does not work

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " \* " 10`
expr: syntax error: unexpected argument ‘\\*’

However I am able to perform the summation though like this

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " + " 10`
55

So why I am getting an error when trying to get the multiplication of a series using expr,can someone explain please??