2

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.

1
  • I would not pipe the output of man blindly like this, since depending on your pager your might have surprises. On my system, my pager is vim, and your command would hang my terminal. At least, use man -P cat $command to use a more trivial pager (here cat). It will also make things slightly more efficient. Commented Dec 11, 2012 at 17:49

2 Answers 2

2

Use built-in command read:

cat commands.txt | while read command; do
  man $command > $command.txt
done
Sign up to request clarification or add additional context in comments.

1 Comment

Use input redirection instead of cat: while read command; do ...; done < commands.txt.
1

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.

2 Comments

This is actually not true, you can man a commands man page into a .txt file just as I mentioned and there are no formatting errors or additions.
May be on your system it may have worked but I tried it on Mac & Linux and it has all kind of extra formatting characters in the output file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.