shellコマンドを読み解く際のTipsをメモします。
はじめに
こんにちは、@bioerrorlogです。
自分の知らない複雑なコマンドに出会ったときにさっと読み解けると、生産性が上がります。 昔は私も無駄にググって時間を浪費してました。
今回は過去の自分に教えるつもりで、コマンドを読み解くときのコツ/流れをメモしておきます。
shellコマンドを読み解く
--helpオプションを使う
とりあえずざっとコマンドの内容を把握するには--help
オプションを使うのがお手軽です。
大体どのコマンドにも--help
オプションは用意されているので、初見のコマンドやよくわからないコマンドに遭遇した時はさっと--help
を見るのがおすすめです。
(仮に--help
が用意されていなくても、だいたいはそのままUSAGE/使用方法が表示されるので問題ありません)
例
$ git --help usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink restore Restore working tree files rm Remove files from the working tree and from the index sparse-checkout Initialize and modify the sparse-checkout examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug diff Show changes between commits, commit and working tree, etc grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches commit Record changes to the repository merge Join two or more development histories together rebase Reapply commits on top of another base tip reset Reset current HEAD to the specified state switch Switch branches tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. See 'git help git' for an overview of the system.
manコマンドを使う
もう少し詳しい説明が欲しい時は、man
コマンドでマニュアルページを表示するのも手です。
--help
の記述よりも詳細な説明を読むことができます。
例: man git
GIT(1) Git Manual GIT(1) NAME git - the stupid content tracker SYNOPSIS git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] [--super-prefix=<path>] <command> [<args>] DESCRIPTION Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. See gittutorial(7) to get started, then see giteveryday(7) for a useful minimum set of commands. The Git User’s Manual[1] has a more in-depth introduction. After you mastered the basic concepts, you can come back to this page to learn what commands Git offers. You can learn more about individual Git commands with "git help command". gitcli(7) manual page gives you an overview of the command-line command syntax. A formatted and hyperlinked copy of the latest Git documentation can be viewed at https://git.github.io/htmldocs/git.html or https://git-scm.com/docs. OPTIONS --version Prints the Git suite version that the git program came from. --help Prints the synopsis and a list of the most commonly used commands. If the option --all or -a is given then all available commands are printed. If a Git command is named this option will bring up the manual page for that command. Other options are available to control how the manual page is displayed. See git-help(1) for more information, because git --help ... is converted internally into git help .... -C <path> Run as if git was started in <path> instead of the current working directory. When multiple -C options are given, each subsequent non-absolute -C <path> is interpreted relative to the preceding -C <path>. If <path> is present but empty, e.g. -C "", then the current working directory is left unchanged. This option affects options that expect path name like --git-dir and --work-tree in that their interpretations of the path names would be made relative to the working directory caused by the -C option. For example the following invocations are equivalent: git --git-dir=a.git --work-tree=b -C c status git --git-dir=c/a.git --work-tree=c/b status -c <name>=<value> Pass a configuration parameter to the command. The value given will override values from configuration files. The <name> is expected in the same format as listed by git config (subkeys separated by dots). Note that omitting the = in git -c foo.bar ... is allowed and sets foo.bar to the boolean true value (just like [foo]bar would in a config file). Including the equals but with an empty value (like git -c foo.bar= ...) sets foo.bar to the empty string which git config --type=bool will convert to false. --exec-path[=<path>] Path to wherever your core Git programs are installed. This can also be controlled by setting the GIT_EXEC_PATH environment variable. If no path is given, git will print the current setting and then exit. Manual page git(1) line 1 (press h for help or q to quit)
manページ内を検索する
manページはかなり長いので、欲しい情報を検索するのも重要です。
文字列を検索するには、manページが表示されている状態で
スラッシュ/
+ 文字列
を入力し、Enterを押すと検索を開始できます。
次の検索ヒットに移動するには n を、
前の検索ヒットに移動するには Shift + n を押下します。
manページの使い方については昔に別途記事にまとめたので、よければそちらもご参考ください。
manコマンドの基本的な使い方を理解する | Linuxコマンド - BioErrorLog Tech Blog
explainshell.comを使う
最後に、explainshell.comという便利なサイトを紹介します。
このサイトでコマンドを打ち込むと、それぞれのコマンド/オプションの該当マニュアルを表示してくれます
自分で調べるのが面倒なほど複雑なコマンドに出会ったら、さっさとこのサイトで調べてしまうのがおすすめです。 大幅に時間を節約できます。
ちなみにこのサイトはオープンソースになっているので、中身を読んでみるのも面白そうですね。
おわりに
以上、shellコマンドを読み解くときのコツをまとめました。
コマンドを毎回ググって調べるよりも、コマンドから提供されている説明にあたるのがおすすめです。 explainshell.comも非常に便利なので、使い倒していきましょう。
どなたかの参考になれば幸いです。
[関連記事]
参考
explainshell.com - match command-line arguments to their help text
GitHub - idank/explainshell: match command-line arguments to their help text