2

I have a C code and I use it to extract file and write in to separate files such as exax0.txt, ..., exax202.txt. To do that I use

for i in $(seq 0 202); 
do echo $i | ./f.out exa-NO-hyp.bin exax${i}.txt <<< "${i}"
done

Instead, I would like to have my output files named as exax495.txt, exax535.txt, ..., exax8545.txt. To do so, I tried something like:

for i in $(seq 0 202); 
do echo $i | ./f.out exa-NO-hyp.bin exax${(i*40+495)}.txt <<< "${i}"  
done

But it says, -bash: exax${i*40}.txt: bad substitution

Can anybody please help me with this?

2
  • 3
    Note that echo "$i" | program ... <<< "$i" is wasting the pipe. The input ends up coming from the here string. Drop the echo "$i" | part. Commented Apr 27, 2015 at 17:05
  • No harm to spend an extra line of code: something like filenumber=xxx and ./f.out exa-no-hyp.bin exa${filenumber}.txt. Commented Apr 27, 2015 at 17:36

1 Answer 1

2

You can use $((...)) to perform arithmetic.

I.e. a simple use would be:

for i in {1..10}; do touch file$(($i+5)).txt ; done

Or in your case:

for i in $(seq 0 202); do echo $i | ./f.out exa-NO-hyp.bin exax$(($i*40+495)).txt <<< "${i}"; done

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

$ is not necessary on variables in $((...)) blocks. and a reference to the Arithemetic Expansion section of the bash reference manual is likely a better than a reference to the (often poor) TLDP ABS guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.