Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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 have a single-thread python script that gets used like this:

python -c "arg"

I also have a *.txt file with many arg, each in one single line in the respective *.txt. A text file, say 1.txt looks as follows:


Now I want to run the python script with all this arguments, one by one as the script does not allow more than one argument.

How can I process this arguments in batch mode using commandline?

share|improve this question
up vote 1 down vote accepted

One way:

for i in *.txt
   while read line
     python -c "$line"
   done < $i
share|improve this answer
Not working. The python script takes the text file as argument, not its content. – mcbetz Jan 10 '13 at 13:33
@mmb : updated the solution – Guru Jan 10 '13 at 13:57
Totally does the job now! – mcbetz Jan 10 '13 at 14:54

A very easy solution,

cat 1.txt | xargs -I{} python -c "{}"

Explanation: cat prints all lines and xargs makes sure each line is used as argument.

Edit: the -I{} means that {} should by replaced by the argument.

share|improve this answer
Wow, that is indeed easy. But does it take into consideration, that the arguments have to be enclosed by ""? – mcbetz Jan 10 '13 at 14:56
Now it does, see my edit ;) If you have multiple files, simply replace 1.txt by *.txt and it should work as well. – Gerhard Burger Jan 10 '13 at 15:07

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.