Day 2 Linux: What are shell built-in commands?

Day 2 Linux: What are shell built-in commands?

Zero To Hero Linux Series

In the vast universe of Linux commands, there exists a category known as shell built-in commands. These are fundamental tools integrated directly into the shell environment, providing essential functionalities for manipulating the shell itself, handling files and directories, managing processes, and more. Understanding these commands is crucial for anyone navigating the Linux command line.

In Linux, we have 4 command types:

  • Built-in Shell Commands

  • Shell Functions

  • Command Alias

  • Executable Programs.

Built-in Shell Commands:

These are commands that are built directly into the shell (like Bash or Zsh) itself. They are part of the shell's functionality and don't require a separate executable file.

Shell built-in commands are those commands that are directly implemented within the shell environment itself, rather than being separate executable files located in directories like /bin or /usr/bin. These commands are interpreted by the shell, which means they are executed more efficiently compared to external commands since there's no need to fork a new process.

Example:

cd (change directory), echo (display text), history (display command history), alias (create aliases).

$ cd Documents
$ echo "Hello, world!"
$ history
$ alias ll='ls -l'

When you run a Linux command, it is run from a binary in a sub-shell by forking the existing process. However, a shell built-in is contained in the shell itself. It runs in the same shell without creating a new process. Thus, shell built-ins are faster.

Ex: cd, pwd, exit, export, alias are some of the commonly used shell built-ins.

Identify the shell built-in command

  1. To identify which command is built-in, you can use the type command as shown:

    type -t command

    For example, if I want to check whether the cd is a built-in command or not, I will be using the following:

     type -t cd
    

    identify the built-in command using the type command in Linux💡

    Sometimes you'll see a re-implementation of a shell built-in as an external command. echo is a common example. To identify, use type -a to see if there is any re-implementation of a built-in command.

     type ll   
     # ll is aliased to `ls -l --color=auto'   
     type cd   
     # cd is a shell builtin 
     type sudo   
     # sudo is /usr/bin/sudo
    

    what if the command is not built-in?

    So what output can you expect from the type command?

    Well, apart from the above output, it can give you four other types of output:

    • alias: If the command is an alias.

    • file: If the command is an executable file and is also configured with the PATH variable.

    • function: If the command is a function.

No output: If the command is not of the above, then, it won't show you anything.

Shell Functions:

Shell functions are similar to scripts, but they are defined within the shell environment rather than being standalone executable files. They are particularly useful for creating reusable pieces of code within shell scripts.

Example:

# Define a shell function
myfunc() {
    echo "Hello, $1"
}
# Call the shell function
myfunc "John"

Executable Programs:

These are standalone programs or scripts stored in executable files. They can be written in any programming language and are invoked by typing their name in the terminal.

Example:

# Execute a Python script
python myscript.py

# Execute a compiled C program
./myprogram

These different types of commands offer flexibility and convenience in managing and interacting with the Linux shell.

Man vs Help Commands: Understanding the Difference

In Linux, users often encounter commands like man and help when seeking assistance or information about specific commands. While both are aimed at providing guidance, they serve different purposes and formats.

Man Command:

The man command stands for "manual" and is used to display the manual pages for various commands, system calls, and library functions in Unix-like operating systems. These manual pages contain detailed information about the command's usage, options, syntax, and often include examples and related commands.

shell built-in commands don't have man pages. If you try using the man command, it will show an error like this:

srinu@centosbox:~$

man cd
# No manual entry for cd
man echo

Example:

man ls

This command displays the manual page for the ls command, offering comprehensive documentation about how to use it effectively.

help echo

You'll have to use the help command that you saw above:

help <shell_built_in_command_name>

It will give you some hints on using the built-in command.

Help Command:

The help command, on the other hand, is a built-in shell command that provides brief information about shell built-in commands. It doesn't provide as detailed information as man pages but offers a quick overview of the command's syntax and usage.

Example:

help cd
ls --help
grep --help
vi --help
nano --help
cp --help

This command displays a concise help message regarding the cd command, showcasing its usage and options.

Conclusion:

Understanding shell built-in commands and how to access help documentation is fundamental for mastering the Linux command line. Shell built-in commands provide essential functionalities directly within the shell environment, making them efficient tools for various tasks. Meanwhile, the man command offers in-depth documentation for external commands, system calls, and library functions, while the help command provides quick assistance for built-in commands. By leveraging these resources, users can navigate the Linux environment with confidence and efficiency.