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)?

4
  • 1
    trap has a man page, just man bash since it's a built-in (which you can know via type trap). Commented Oct 6, 2023 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. Commented Oct 7, 2023 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. Commented Oct 13, 2023 at 10:22
  • @TheNotoriousGBR This is exactly what I need. Thanks! Commented Nov 11, 2023 at 6:24

1 Answer 1

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:

10
  • 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
    Commented Oct 6, 2023 at 14:01
  • 1
    If you want to avoid security risks then don't use eval, and don't use unquoted variables Commented Oct 6, 2023 at 17:53
  • 1
    AntonSamokat I've fixed your code again; you were right when using @ilkkachu's code but not when you removed the double quotes Commented Oct 6, 2023 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 Commented Oct 6, 2023 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
    Commented Oct 6, 2023 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .