A command in a Linux shell can be one of four things:

  1. An executable program: This is a file that can be run as a program. This includes compiled binary programs (like those written in C++) and programs written in scripting languages (such as the shell itself, Perl, or Python).
  2. A shell builtin: A command that is built directly into the shell program itself. For example, the cd command is a shell builtin.
  3. A shell function: A small script that’s integrated directly into the shell’s environment.
  4. An alias: A custom command you define yourself that acts as a shortcut for another command or sequence of commands.

Identifying Commands

You can use the following commands to determine the type and location of a command:

  • type: This is a shell builtin that tells you how the shell will interpret a command name. For example, type ls might show you that ls is an alias, while type cp indicates it’s an executable program at a specific path, like /usr/bin/cp.
  • which: This command only works for executable programs. It tells you the exact location of the executable file that the shell will run. For example, which ls will output /usr/bin/ls.

Command Documentation and Help

Linux provides several ways to get documentation for commands:

  • help: Use this for information on shell builtins. You type help followed by the command name, like help cd.
  • --help: Many executable programs support this option to display a brief summary of the command’s syntax and options. You can use it like mkdir --help.
  • man: This command displays a program’s man (manual) page. Man pages are formal reference documents that contain a synopsis, description, and list of options. They are not tutorials and typically don’t include examples. man pages are organized into sections, such as section 1 for user commands and section 5 for file formats. To access a specific section, you use man section number command, such as man 5 passwd.
  • apropos: This command searches the names and one-line descriptions of man pages for a given keyword. It’s useful when you don’t know the exact name of a command.
  • whatis: This command provides a one-line summary from a command’s man page.
  • info: The GNU Project provides an alternative to man pages called “info” pages. These documents are hyperlinked and structured like a tree, allowing for more in-depth, connected information. You can use commands like n for next node, p for previous, and q to quit.

Creating our Own Commands with alias

You can create your own custom commands using the alias command.

  • Putting commands on one line: You can combine multiple commands on a single line by separating them with a semicolon (;). For instance, cd /usr; ls; cd - will change to /usr, list the files, and then return to your original directory.
  • Creating an alias: The syntax is alias name='string'. The name is the new command you’re creating, and the string contains the command(s) you want to run. The string must be enclosed in quotes. For example, alias foo='cd /usr; ls; cd -'.
  • Viewing aliases: To see all aliases currently defined in your shell, you can simply type the alias command without any arguments.
  • Removing an alias: To remove a temporary alias you’ve created, use the unalias command, such as unalias foo.
  • Persistence: Aliases created on the command line are temporary and will be gone when your shell session ends.