Command Types

Command Types

  • Try the following commands:
1type -a ls
2which ls
  • The bash type builtin shell command can show you what is the type of the parameter you give it. The -a parameter asks for ALL available types.
  • Linux has several command types(e.g ‘alias’, ‘function’, ‘builtin’, ‘file’, ‘keyword’)
  • We'll focus on:
    • file shell running executable files
    • builtin command that is part of the shell itself.
  • Here's what I saw in my linux system:
1$ type -a ls
2ls is aliased to `ls --color=auto'
3ls is /usr/bin/ls
4ls is /bin/ls
  • So first, ls is an alias (so another name) for a more complicated version of the command (with some coloring parameters)
  • Then, ls is just an executable file, located twice in these directories.

Getting more details of these files:

1$ ls -l /usr/bin/ls
2-rwxr-xr-x 1 root root 147176 Sep 24  2020 /usr/bin/ls
  • So we see a file that has 147176 bytes in size. If this an executable file?
    Let's find out (using the file command):
1$ file /usr/bin/ls
2/usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=6e45645c07d892b7b01c04b0522cf5b0e10c9e52, for GNU/Linux 3.2.0, stripped
3$

We'll cover the ELF format in later posts, but we can see that the ls command is actually a program.

EACH TIME YOU TYPE LS - YOU ARE RUNNING A PROGRAM !!!

PATH

But how does linux know where to find the ls executable file?

  • The shell has a list of directories that it looks for program in.
  • This list is called path, and it is stored in a variable called PATH.
    (we will cover variables in later posts)
  • In order to see this list, use the following command:
1echo $PATH
  • Directories are seperated from each other by using a colon (:)
  • We'll learn how to change the PATH variable when we learn about shell variables.

Shell builtins

  • What about the cd (change directory) command?
    Is it also a program, external to the shell?
  • Try the type command with cd
  • As you can see, the cd operation is part of the shell itself, not an external command.
  • There are many other shell builtins (for example type is one of them)
  • echo is also a bash builtin.