This is a test version of Biostars. For the public version, visit https://www.biostars.org.
awk command in shell script

I have an awk command which runs well on terminal: this awk command creates diffrent file according to their column header. awk command:

for((i=2;i<5;i++)); do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i > n".txt"}' ${Batch}.result.txt; done

the same command when incorporated in a shell script shows error:

Syntax error: Bad for loop variable
awk shell linux

Are you sure that you are executing your script with bash and not sh or dash? The latter don't like some of the bash syntax.

please post output from echo $SHELL

Since you're using my answer from your other thread, could you please go mark it as the accepted answer?

Hi, I have already accepted the answer. Thank you 😊

Hello mittu1602!

We believe that this post does not fit the main topic of this site.

sorry, I should have flagged this before instead of answering ; this post is not related to bioinformatics; It's just related to linux

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

1 answer

i suppose you bash is sh:

$ sh
$ for((i=2;i<5;i++));  do echo $i ; done
sh: 1: Syntax error: Bad for loop variable

while it should be bash

$ bash
$ for((i=2;i<5;i++));  do echo $i ; done
2
3
4

add a shebang at the start of your script and make it executable with chmod +x

Alternatively

for i in $(seq 2 4); do ..; done

or

for i in 2 3 4; do ..; done

OP is probably using Ubuntu so his sh points to dash instead of bash

Log in to answer this question.