mr.pribesh
mr.pribesh

Reputation: 187

passing a list of variables to simple bash command

I am looking to pass a list of variables from a text file into a simple bash command. Essentially I have a list of all linux commands (commands.txt). I want to pass each command from that file (each is on a new line) to the man command and print each to a new .txt file so each variable would be passed to this:

man $command > $command.txt

so each variable would have its man page printed to its name .txt. Please help! I would like to do this in bash script but anything that will work would be appreciated.

Upvotes: 2

Views: 1072

Answers (2)

anubhava
anubhava

Reputation: 785088

You CANNOT redirect output of man page into a file like this:

man man > man.txt # DON'T DO THIS

Problem with above is that the output man.txt file will contain a lot of extra formatting characters as well.

Here is correct way to write your script:

while read command; do
   man $command | col -b > $command.txt
done < commands.txt

Note use of col -b here to remove all formatting characters.

Upvotes: 1

yasu
yasu

Reputation: 1364

Use built-in command read:

cat commands.txt | while read command; do
  man $command > $command.txt
done

Upvotes: 2

Related Questions