Shell commands

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

← The bash shellHomeThe role of shells in the Linux environment →

The bash shell comes with two types of commands:

  • Internal commands (builtins) - part of the shell itself, i.e. built into the shell.
  • External commands - separate binaries stored in /sbin, /usr/sbin, /usr/bin, /bin, or /usr/local/bin directories.

Executing commands using the Bash Shell

As explained earlier, bash interprets commands typed in by the user at $ prompt. Each text typed into the bash may have three parts:

  1. The command
  2. Options that typically start with a - or --
  3. Argument

Make sure each word typed into the bash is separated from each other with white space. For example:

ls
ls  /etc/
ls -l  /etc/

The following are invalid as you forgot to separate part of command with white space:

ls/etc/ 
ls-l/etc/

And when you are ready to run a command such as ls, press the Enter/Return key.

Examples

The date command shows the Linux system date and time on screen:

date

Next, display the contents of file named /etc/hosts, and /etc/resolv.conf, run:

cat /etc/hosts
cat /etc/resolv.conf

To list /etc/ directory contents in a long listing format, type:

ls -l /etc/

Let us view all hidden files in your home directory using ls:

ls -a $HOME
ls -l -a $HOME

Bash and Command Types

The bash shell understands the following types of commands:

  • Aliases such as ll
  • Keywords such as if
  • Functions (user defined functions such as genpasswd)
  • Built in such as pwd
  • Files such as /bin/date

The type command can be used to find out a command type.

type command

The type command can be used to find out if a command is built in or an external binary file.

Find out if ls is built in or an external command

Type the following command at a shell prompt:

type -a  ls

Sample Output:

ls is /bin/ls

To find out if history command is built in or an external command, enter:

type -a  history

sample Output:

history is a shell built in

However, some commands are supplied as both internal and external commands. For example:

type -a true
type -a echo

sample Outputs:

echo is a shell built in
echo is /bin/echo

List of command bash keywords and built in commands

← The bash shellHomeThe role of shells in the Linux environment →