Bash multiplication and addition

154,220

Solution 1

Using arithmetic expansion:

for (( k = 0; k < 50; ++k )); do
  a=$(( 2*k + 1 ))
  echo "$a"
done

Using the antiquated expr utility:

for (( k = 0; k < 50; ++k )); do
  a=$( expr 2 '*' "$k" + 1 )
  echo "$a"
done

Using bc -l (-l not actually needed in this case as no math functions are used):

for (( k = 0; k < 50; ++k )); do
  a=$( bc -l <<<"2*$k + 1" )
  echo "$a"
done

Using bc -l as a co-process (it acts like a sort of computation service in the background¹):

coproc bc -l

for (( k = 0; k < 50; ++k )); do
  printf "2*%d + 1\n" "$k" >&${COPROC[1]}
  read -u "${COPROC[0]}" a
  echo "$a"
done

kill "$COPROC_PID"

That last one looks (arguably) cleaner in ksh93:

bc -l |&
bc_pid="$!"

for (( k = 0; k < 50; ++k )); do
  print -p "2*$k + 1"
  read -p a
  print "$a"
done

kill "$bc_pid"

¹ This solved a an issue for me once where I needed to process a large amount of input in a loop. The processing required some floating point computations, but spawning bc a few times in the loop proved to be exceedingly slow. Yes, I could have solved it in many other ways, but I was bored...

Solution 2

You can simplify:

a=$(($((2*$k))+1));

to:

a=$((2*k+1))

Solution 3

You can use the let command to force a calculation.

let a="2*k+1"

Note that we don't need $k in this structure; a simple k will do the job.

Share:
154,220

Related videos on Youtube

AVS
Author by

AVS

Updated on September 18, 2022

Comments

  • AVS
    AVS almost 2 years
    for k in {0..49};
    do
    a=$(($((2*$k))+1));
    echo $a;
    done
    

    Hi, I need a simplified expression for the third line, maybe one that does not use command substitution.

    • Admin
      Admin almost 8 years
      @Theophrastus: As suggested it works fine but what if i wanted to use expr instead of (()).
    • Admin
      Admin almost 8 years
      This is bash and not C, so remove all ; - unless you write it in a singular line.
    • Admin
      Admin almost 8 years
    • Admin
      Admin almost 8 years
      declare -i a; for k in {0..49}; do a=2*$k+1; echo $a; done
    • Admin
      Admin over 6 years
      Aside: $(( ... )) is arithmetic expansion not command substitution.
    • Admin
      Admin about 5 years
      The whole loop can be written seq 1 2 99 or printf '%s\n' {1..99..2}
  • Stéphane Chazelas
    Stéphane Chazelas almost 8 years
    That fails if there's a file called a=2whateverk+1 in the current directory. Worse, if there's a file called a=2+b[$(reboot)]k+1, that calls the reboot command. Best is to use ((...)) here (((a = 2 * k + 1))), or the POSIX syntax: a=$((2 * k + 1))
  • Stephen Harris
    Stephen Harris almost 8 years
    We can quote it; let a="2*k+1" to solve that.