Home Looping through the content of a file in Bash
Reply: 0

Looping through the content of a file in Bash

user7091
1#
user7091 Published in September 20, 2018, 1:19 pm

How do I iterate through each line of a text file with Bash?

With this script:

echo "Start!"
for p in (peptides.txt)
do
    echo "${p}"
done

I get this output on the screen:

Start!
./runPep.sh: line 3: syntax error near unexpected token `('
./runPep.sh: line 3: `for p in (peptides.txt)'

(Later I want to do something more complicated with $p than just output to the screen.)


The environment variable SHELL is (from env):

SHELL=/bin/bash

/bin/bash --version output:

GNU bash, version 3.1.17(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

cat /proc/version output:

Linux version 2.6.18.2-34-default (geeko@buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Mon Nov 27 11:46:27 UTC 2006

The file peptides.txt contains:

RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL
share|improve this question
  • 7
    Oh, I see many things have happened here: all the comments were deleted and the question being reopened. Just for reference, the accepted answer in Read a file line by line assigning the value to a variable addresses the problem in a canonical way and should be preferred over the accepted one here. – fedorqui Aug 30 '16 at 9:44

11 Answers 11

active oldest votes
up vote 1549 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.529595 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO