Understanding the Linux shell
The shell is a program that interprets and manages commands
The shell provides a way to create executable script files, run programs, work with file systems, compile computer code, and manage the computer.
There are different shells in use today. We will be using BASH which is an acronym for Bourne Again Shell. Bash shell was named after Stephen Bourne and is represented by the sh command. bash is the default for most Linux systems.
Other shells listed:
C Shell (csh) - popular among BSD UNIX users
Korn shell (ksh) - Popular Among UNIX System V users
Dash shell - popular among Debian users
tsch shell - improved C shell, Bourne look-alike)
rbash shell - Restricted shell is a Unix shell that restricts some of the capabilities available inside interactive user session
Examples of Shell interfaces: shell prompt, terminal window, virtual console.
Shells terminal will start with username@hostname:~$
the $ prompt indicates this is a regular user
the # prompt indicates this is a root user, similar to admin user in windows
Using a terminal window:
Applications > utilities > terminal
The top-left icon will add a new terminal tab.
Choosing your shell:
open your terminal and type the following command, which will show your user name.
$: who am i
Without quotations, shows the definition of your user account, shows /bin/bash as the default shell.
$: grep "username" /etc/passwd
Shows you what shells are available on your system.
$: cat /etc/shells
Shows you the path to the shell you are using.
$: which bash
Example from terminal window inside of our linux shell:
Username@hostname:~$ cat /etc/shells
# /etc/shells: valid login shells
Username@hostname:~$ which bash
You can switch shell you want to use in terminal simply typing in the name of the shell you want.
While in the terminal you can’t use the ctrl+v or ctrl+c for copy paste.
You will have to use the clipboard at the top of the screen.
In the box, paste the text you want on the shell clipboard, and click the OK button. Then right-click on the cursor and paste the text you want.
You can get documentation for commands, file formats, and other components in Linux.
$: man bash (documentation of commands)
$: man bash > output_textfile1.txt (this prints the man bash to txt)
$: press ctrl+d to stop script output
$: date (results with day of week, month, date, time, time zone, & year)
$: date - -help (shows different format indicators you can use)
$: pwd (shows your current working directory)
$: hostname (shows the computer name)
$: ls (shows files and directories in your current working directory)
Understanding command syntax:
With most commands, you can add one or more options to change the command's behavior. example below
$: ls -l -a -t
$: ls -lat
ls (list what is inside of a directory)
-l (long listing)
-a (show hidden dot files)
-t (list by time)
Some commands also accept arguments after certain options are entered or at the end of the entire command line.
Definition of argument is an extra piece of information, such as a file name, directory, username, device, or other items that tell the command on what to act on.
Displays the contents of the /etc/passwd. The argument here is /etc/passwd.
$: cat /etc/passwd
arguments can be associated with an option. the argument follows immediately after the option.
Example: results with the directories under Desktop.
$: ls --hide=Desktop
Single-letter options the argument follows a space. full word options, the argument follows an equal sign (=)
$: tar -cvf backup.tar /home/chris (this results with backup.tar file inside directory /home/chris)
c – create file
v – short for verbose – tells the shell to show all lines in a script while they are read, it activates verbose mode.
f – named backup.tar
$: uname (shows the type of system you are on)
$: uname -a (adding the -a will show the hostname, kernel release, and kernel version)
The ls command by itself shows all the regular files and directories in the current directory. When you add the -a, you can also see the hidden files in the directory.
$: date (prints the current day, date, and time)
$ date - -help (shows different format indicators you can see)
$ id (shows numeric user id-uid, group id-gid, groups id shows permission and access to different groups/applications/resources, in Linux version of fedora and RedHat-Linux users have the same primary group name as their username.))
$ who (shows your username and date of first onset)
$ who -uH
-u ask to add information about idle time and the process ID
-H asks that a header be printed.
The who command shows who the user is when the login session began. idle time shows the shell has been open without any command being typed, the dot indicates it's currently active. PID shows the process ID of their user’s login shell. COMMENT shows the name of the remote computer the user had logged in from, and If that user logged in from another computer or network.
To find commands you type, the shell finds the commands you type. to find commands you type the shell looks in what is referred to as your path. For commands that are not in your path, you can type the complete identity of the location of the command.
To find the command you typed, the shell looks to your path.
For commands you didn’t type, you can type the complete identity of the location of the command.
If you know the directory of the command you want to run, one way to run it is type the full/absolute path to the command.
example: run date from the bin directory
Thu 28 Jan 2021 01:07:44 AM UTC
cons: inconvenient if the path is much longer
solution: put the command into a directory that is well known then add it to your shell's path environment variable
$ echo $PATH (shows your current path)
example from the terminal:
$: echo $PATH
The above shows the common path for a regular Linux user and the directories are separated by the colons. Most user commands are stored in the /bin, /usr/bin, or /usr/local/bin directories
Some Linux systems don’t put directories in regular user's paths
to make the command available to all users put it in /usr/local/bin
If you are adding your commands or shell scripts, place them in /home/YourUsername/bin. most user commands that come with Linux are stored in the following locations.
/sbin and /usr/sbin are directories used for administrative commands.
By default, Linux will immediately begin searching the path. Executables in the current directory run only if they are in the PATH variable or give an absolute, or relative.
The order of the path directory is important. Directories are checked from left to right. If a command is in the two directories, Linux will read the command coming from the first directory listed. You can choose which directory Linux reads the command from, by changing path variable or type the full path.
Some commands are built into the shell, and others cab be overridden by creating aliases that define any commands and options in the command to run. There are ways to of defining a function that consists of a stored series of commands.
Below is the order in which the shell checks for the commands you type:
1. Aliases. this enables you to define a short name or long, complicated command. In terminal type alias to see what aliases are set.
2. Shell reserved word. Many of these reserved by the shell are used in programming-type functions, like in "while", "case", and "else" Function. a set of commands that are executed together within the current shell.
3. Function. A set of commands that are executed together within the current shell.
4. Built-in command. this type of command is built into the shell, some of the most common examples of this are listed below.
cd (to change directories)
echo (to output text to the screen)
fg (to bring commands that were previously run)
pwd (to list the present working directory
set (to set shell options)
type (to show the location of a command)
5. File system command. This is stored in and executed from the computer's file system. These are commands that are indicated by the value of the PATH variable.
You can find where a command comes from with the following commands:
The "type" command is used to find a particular command is taken from.
The "which" utility only searches your existing path for files,
The "find" is a much more flexible utility for searching any path you desire for any type of file you can specify.
The "locate" command, which uses an indexed database of files on your system to quickly find a file anywhere on your system
$: type bash
bash is /usr/bin/bash
$: which bash
$: type case
case is a shell keyword
$: type which
which is hashed (/usr/bin/which)
$: type return
return is a shell builtin
$: type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
adding an "-a" option will list all the know locations of the command printed.
Sometimes you run a command and receive an error message that the command was not found or that permission To run the command was denied. If the command was not found, check that you spelled the command correctly and that it is located in your PATH variable. If permission to run the command was denied, the command may be in the PATH variable, but may not be executable. Also, remember that case is important, so typing CAT or Cat will not the cat command.
The locate utility may or may not be installed on your Linux distribution. to install, open terminal, and type the following commands.
$: sudo apt update
$: sudo apt install mlocate
Please sign in to leave a comment.