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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 9 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 script.sh file another_file

Extra idea. Use readarray:

readarray -t LINES < "$FILE"
for LINE in "${LINES[@]}"; do
share|improve this answer

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 – iruvar 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 '15 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 '15 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.