0

For example, there is no manual pages for trap command. Running:

man trap

will give:

No manual entry for trap

Running:

trap --help

will print help straight to bash console without using less like man do. And it is not handy to clog up console output. It is more user friendly to redirect help information to less:

trap --help | less

But it is not handy to print such quite long command each time to get help. How to configure it more shortly (something as alias) - for example, to use it like help2 trap (help2 because probably it is not right to override existing help command behavior) or better more shortly just hp trap (hp is short meaning for help word)?

  • 1
    trap has a man page, just man bash since it's a built-in (which you can know via type trap). – Ginnungagap Oct 06 '23 at 20:36
  • Is there a way to jump directly to description of specific command, for example trap in man bash? I found it with less search by trap keyword but this takes time. – Anton Samokat Oct 07 '23 at 13:58
  • 1
    It's not less typing, but this works for me.

    man -P "less '+/^ *trap'" bash-builtins

    You might turn this into a shell function, parameterized in the page name and search string.

    – TheNotoriousGBR Oct 13 '23 at 10:22
  • @TheNotoriousGBR This is exactly what I need. Thanks! – Anton Samokat Nov 11 '23 at 06:24

1 Answers1

2

Add the following function to ~/.bashrc:

# Show help for command in less like man. Usage: hp cmd. For example:
# hp trap
hp() { "$@" --help | less; }

Source it to make visible in the opened bash console (or just reopen console):

source ~/.bashrc

Usage:

hp some_shell_command

Example of usage:

hp trap

Update

hp() { "$1" --help | less; } replaced to hp() { "$@" --help | less; } to support commands with script parameter like python test.py --help

I checked the following cases (with Python 3.8.10):

  • hp python # executed as: python --help | less
  • hp python test.py # executed as: python test.py --help | less
  • hp python "te st.py" # executed as: python "te st.py" --help | less
  • hp ./test # executed as: ./test.py --help | less
  • hp "./te st" # executed as: "./te st.py" --help | less

All this cases are working. Thanks to ilkkachu and Chris Davies for theirs great advices!

Used test.py and "te st.py" with the following same content:

#!/bin/python

Python code here taken from the following answer on the question:

How do I access command line arguments? [duplicate]

https://stackoverflow.com/a/42929351/1455694

import argparse

parser = argparse.ArgumentParser("simple_example") parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int) args = parser.parse_args() print(args.counter + 1)

Output for hp python "te st.py":

usage: simple_example [-h] counter

positional arguments: counter An integer will be increased by 1 and printed.

optional arguments: -h, --help show this help message and exit


Related questions about help, man, info commands:


This solution made with the help of the questions:

  • 1
    the proper way to do that would be something like hp() { "$1" --help | less; }, which would work even if the command was something like ./test release/foo. (Of course a command like that would be awkward to use and would need to be quoted, so it's likely not often an issue, but in this case it's not too hard to write the function properly, and it's the same issue that could come up with arbitrary filenames.) – ilkkachu Oct 06 '23 at 14:01
  • 1
    If you want to avoid security risks then don't use eval, and don't use unquoted variables – Chris Davies Oct 06 '23 at 17:53
  • @ilkkachu I tested hp() { "$1" --help | less; } function with hp "python test.py" command. It gave "python test.py: command not found" error. Removing quotation marks from $1 has helped. With hp() { $1 --help | less; } the following cases are working: hp pwd, hp ./test.py, hp "python test.py". I updated the answer. – Anton Samokat Oct 06 '23 at 19:09
  • @ChrisDavies probably in this particular case security risks will not appear. I had to remove quotation marks in order to fix case with spaces: hp "python test.py". How do you think hp() { $1 --help | less; } is appropriate solution? – Anton Samokat Oct 06 '23 at 19:17
  • @ilkkachu Thanks for your one-liner. It is more nice. I added it to the answer. – Anton Samokat Oct 06 '23 at 19:25
  • 1
    AntonSamokat I've fixed your code again; you were right when using @ilkkachu's code but not when you removed the double quotes – Chris Davies Oct 06 '23 at 19:39
  • 1
    You wrote, "It gave "python test.py: command not found" error." - do you have a file called python test py (a single file that has a space in its name)? If not, the error message is correct – Chris Davies Oct 06 '23 at 19:42
  • 1
    @AntonSamokat, ok, right, I saw the code using just $1 and made the wrong assumption that passing other arguments wasn't required. If we want to allow that, then it would be hp() { "$@" --help | less; }, which would properly work with e.g. hp python3 "./test version/foo.py". – ilkkachu Oct 06 '23 at 20:08
  • 1
    In general, the point is that when the command line is first processed, the shell parses quotes and such and turns the command line into a list of distinct strings ("words") and there's no need to process them any further. Just use the list as-is. Doing that allows acting on arbitrary strings the normal way, with the user just having to quote them using the normal shell syntax, just like they would if they called ls on some file with an odd name. Expanding variables outside quotes invokes word-spltting, and just breaks strings with whitespace (or glob chars), with no way to fix it. – ilkkachu Oct 06 '23 at 20:16
  • Gentlemen, I absolutely love your great comments. I put "$@" to the function in answer and added Update section with use cases and test results. Thanks for the tips! – Anton Samokat Oct 07 '23 at 13:44