Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

shell - Read file line by line and perform action for each in bash

I have a text file, it contains a single word on each line.

I need a loop in bash to read each line, then perform a command each time it reads a line, using the input from that line as part of the command.

I am just not sure of the proper syntax to do this in bash. If anyone can help, it would be great. I need to use the line from the test file obtained as a paramter to call another function. The loop should stop when there are no more lines in the text file.

Psuedo code:

Read testfile.txt.
For each in testfile.txt
some_function linefromtestfile
See Question&Answers more detail:os

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about:

while read line
   echo $line
   // or some_function "$line"
done < testfile.txt

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers


50.5k users