简体   繁体   中英

Bash take variable to a power

I'm having trouble using online references to solve this, I need to take a power of a variable as an argument like so:

for i in `seq 0 4`
do
    ... --chunk_size=$((awk `BEGIN{print 10^$i}`))
done

But this does not work and gives me an error, how can I do this?

Simply use Bash with **

for i in {0..4}
do
    ... --chunk_size="$(( 10 ** i ))"
done

Example:

$ for i in {0..4}; do echo "$(( 10 ** i ))"; done
1
10
100
1000
10000

Using bc :

for i in {0..4}; do bc -l <<<"10^$i"; done
1
10
100
1000
10000

using awk :

for i in {0..4}; do awk 'BEGIN{print 10^'$i'}'; done
1
10
100
1000
10000

So your code could look like this:

for i in `seq 0 4`
do
    ... --chunk_size=$(bc -l <<< "10^$i")
done

or

for i in `seq 0 4`
do
    ... --chunk_size=$(awk 'BEGIN{print 10^'$i'}')
done

use -v option for pass bash variable to awk

for i in `seq 0 4`
do
    chunk_size=$(awk -v"i=$i" 'BEGIN{print 10^i}');
    echo $chunk_size
done

OUTPUT:

1
10
100
1000
10000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM