Getting started on the Linux (or Unix) command line, Part 2


Even after you’ve used Linux for a while, you will still find yourself needing help from time to time, whether you’re learning a new command or you need more details on some of the command’s numerous options. So let’s examine some of the help that you can get from the system itself.

Commands that provide help

Among the most useful commands for getting help on Linux is the man (i.e., manual) command that provides information on what a command does and what options are available. Almost every command will have a man page available that you can use to view a screenful at a time. To view a man page, you would simply use a command like “man ls” or “man date”.

The command below displays the man page for the date. In the command output shown below, the content is passed to the head command to limit the display to the top eleven lines. I did this so that I didn’t need to include the entire man page in this post!

$ man date | head -11
DATE(1)                         User Commands                           DATE(1)

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display date and time in the given FORMAT.  With -s, or with [MMDDhhmm[[CC]YY][.ss]], set the date and time.

Another helpful command is simply named “help”. This command provides information on bash built-ins, which are commands that are embedded in the bash executable and, because of this, don’t have their own man pages.

To start with this, the command below provides no information on the date command because it is not a builtin. Don’t be surprised if you get this response from time to time.

$ help date
-bash: help: no help topics match `date'.  Try `help help' or `man -k date' or `info date'.

If you type “help help”, you will get output like this:

$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.

    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.

    Options:
      -d        output short description for each topic
      -m        display usage in pseudo-manpage format
      -s        output only a short usage synopsis for each topic matching
                PATTERN

    Arguments:
      PATTERN   Pattern specifying a help topic

    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.

To get a list of bash builtins, you can use a command like the help command by itself. Be forewarned that the response will be quite overwhelming for a Linux newbie. Only the first lines of the output are shown below. This output is followed by the list with descriptions of each builtin.

$ help | head -7
GNU bash, version 5.2.15(1)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

The output below makes it clear that the help command is itself a bash builtin.

$ help | grep help
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
 help [-dms] [pattern ...]	<=== it's in the builtins list

The complete output will contain more than 40 lines, but you can view it a screenful at a time if you pipe the output to the more command like this:

$ help | more

Lines in the rest of the output will look like this – using a 2-column format:

alias [-p] [name[=value] ... ]                      logout [n]

As you may have noticed in the help command output above, you can also make use of the man -k command to provide short descriptions of commands related to the search term. Here’s an example that has only a single description. Most search terms will generate many more command descriptions.

$ man -k calendar
cal (1)              - display a calendar

Using the echo command

The echo command is one that will repeat what you type. If you use a string, it will simply display it. If you ask it to display a variable, on the other hand, it will display the variable’s value. In the second command below, the value of the $SHELL variable is the shell that is being used.</strong<echo<>

$ echo hello!
hello!
$ echo $SHELL
/bin/bash

You can also determine which shell you are using by looking at your entry in the /etc/passwd file. It’s the last field in the colon-separated line that describes your account. You can also see the numeric userid and groupid – both 1004 in this example.

$ grep justme /etc/passwd
justme:x:1004:1004:Just Me:/home/justme:/bin/bash

Saving command output to a file

To save the output of a command to a file, use a > sign to redirect the output. In the example below, the output from the date command is sent to a file named “times” – overwriting the file if it already exists. The second adds to it.

$ date > times
$ date >> times
$ cat times
Wed Nov  8 03:15:18 PM EST 2023
Wed Nov  8 03:15:24 PM EST 2023

Wrap-up

The more you get used to the Linux command line, the more you are likely to appreciate how easy it is to get a lot of work done without a lot of effort. Part 3 of this intro to Linux will introduce a series of very useful commands.



Source link