Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. The output of the command is written to another file. How do I go about doing that? I don't know where to start.

share|improve this question

5 Answers 5

up vote 8 down vote accepted

Use while read loop:

: > another_file  ## Truncate file.

while IFS= read -r LINE; do
    command --option "$LINE" >> another_file
done < file

Another is to redirect output by block:

while IFS= read -r LINE; do
    command --option "$LINE"
done < file > another_file

Last is to open the file:

exec 4> another_file

while IFS= read -r LINE; do
    command --option "$LINE" >&4
    echo xyz  ## Another optional command that sends output to stdout.
done < file

If one of the commands reads input, it would be a good idea to use another fd for input so the commands won't eat it:

while IFS= read -ru 3 LINE; do
done 3< file

Finally to accept arguments, you can do:



exec 4> "$ANOTHER_FILE"

while IFS= read -ru 3 LINE; do
    command --option "$LINE" >&4
done 3< "$FILE"

Which one could run as:

bash file another_file

Extra idea. Use readarray:

readarray -t LINES < "$FILE"
for LINE in "${LINES[@]}"; do
share|improve this answer
Thank You! this is the most complete answer I ever recieved. I used the idea that allows accepting arguments! This was veeery helpful! – WojciechF Aug 11 '14 at 17:35
@WojciechF Welcome. If you find the answer most appropriate to you question please accept it by clicking the check mark thanks :) – konsolebox Aug 11 '14 at 17:37

Another option is xargs.


cat file | xargs -I{} -d"\n" command --option {} other args

{} is the place holder for the line of text.

I am using gnu xargs, on gnu+linux. Other xargs may not have all of the same features.

share|improve this answer
reducible to <file xargs -L 1 -I{} command --option {} other args – 1_CR Aug 12 '14 at 12:13
    sed "s/'/'\\\\''/g;s/.*/\$* '&'/" <<\FILE |\
    sh -s -- command echo --option
all of the{&}se li$n\es 'are safely shell
quoted and handed to command as its last argument
following --option, and, here, before that echo


--option all of the{&}se li$n\es 'are safely shell
--option quoted and handed to command as its last argument
--option following --option, and, here, before that echo
share|improve this answer

The best answer I found is:

for i in `cat`; do $cmd $i; done < $file
share|improve this answer
This will invoke the command once for each word in the file.  Also, you should always quote references to shell variables (as in do "$cmd" "$i";) unless you have a reason not to; if the file contained a * as a word by itself, your code would run $cmd *, which, of course, would run the command with a list of the files in the current directory. – G-Man Jun 7 at 5:13
ed file.txt
%g/^/s// /
chmod 755 file.txt

Take all the lines of a file and pass them as arguments to a single command i.e.,

command line1 line2 line3 ....

If you need the --option flag to precede each line change the second command to:

%g/^/s// --option /
share|improve this answer
What -1 for using ed...really? – user2217522 Nov 24 '14 at 4:58
I didn't downvote you, but the person who did probably had these reasons: (1) This doesn’t do what the question asks for.  This invokes the command once with all the contents of the file on the command line; rather than once per line.  (2) This does nothing to handle characters that are special to the shell (that might be in the file); e.g., ', ", <, >, ;, etc.  (3) This creates an unnecessary temporary file.  (4) Things like this are generally done with “here documents”.  (5) Your ed commands are clumsy; the first two commands can be reduced to %s/^/ / and %j. – G-Man Jun 7 at 5:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.