Bash multiplication and addition

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.

Replay

Using arithmetic expansion:

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

Using expr:

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...

You can simplify:

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

to:

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

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.

Category: bash Time: 2016-07-30 Views: 3

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 3.459 (s). 13 q(s)