Ch 1 - Introduction
A shell is a user interface for accessing an operating systems services such as :
⇒ File management
⇒ Process management
⇒ Batch processing
⇒ Operating system monitoring and configuration
It is know as a shell because it is the outermost layer around the operating system.
.In General, There are two types of Operating system shells :
⇒ Command line shells – provide a command-line interface (CLI) to the operating system
⇒ graphical shells – provide a graphical user interface (GUI)
.NOTE:
The command-line interface is often referred to as the console, the terminal or simply shell.
So, When we say shell, we will be referring to the CLI, though note that there can be a graphical version of the shell as well.
.shell = command line interface ( though one can argue )
.Operating system shells
⇒ In Windows – MS-DOS prompt – C:\>
⇒ In OS X – Bash shell – ozesh-mac:~ oz$
⇒ In Linux – Bash shell – ozesh@warcraft-asus:~$
.The Command Line Interface or CLI is an interface, where the user types a command ( a sequence of characters – typically a command followed by its parameters ) and executes it by pressing the Return key.
.The program which handles the interface is called a command-line interpreter or command-line-processor.
.Usage and benefits
⇒ Requires fewer system resources
⇒ Automation of repetitive tasks simplified by line editing
⇒ History mechanism for storing frequently used sequences.
⇒ System administration and maintenance
.Types of CLI
⇒ Operating system command-line interfaces (helps access the operating system services)
⇒ Application command-line interfaces (helps access the services provided by the application)
.In 1964 – The shell
An MIT Computation Center staff member Louis Pouzin developed the RUNCOM tool for executing command scripts while allowing argument substitution. Pouzin coined the term ‘shell‘ to describe the technique of using commands like a programming language ( commands used as building blocks for writing more commands).
.Later on, after going through Christopher Strachey, a British scientist’s macro-generator design, he wrote a paper on how to implement the idea in the Multics operating system
.In 1965 – Schroeder’s Multics shell
After Pouzin returned to his native France in 1965, An American Software engineer – Glenda Schroeder, noted and implemented the first command-line user interface shell.
.In 1971 – The Thompson shell
Modeled after Schroeder’s Multics shell, Ken Thompson wrote the first Unix shell ( V6 shell ) that was introduced in the first version of Unix.
It was a simple command interpreter, not designed for scripting.
It introduced several innovative features to the command-line interface and let to the development of the later Unix shell.
.Mid-1975 – The Mashey shell (PWB shell)
It was a modified version of the Thompson shell that was maintained by John Mashey, Dick Haight, Alan Glasser.
It was released with Version 5 Unix and was available till Version 6 Unix.
.In 1977 – The Bourne shell (sh)
It was developed by Stephen Bourne at Bell Labs and was used as the default shell for Version 7 Unix as a replacement for the V6 shell.
.It was commonly used as an interactive command interpreter and as a scripting language. It contains most of the features that are commonly considered to produce structured programs.
.Bourne shell let to the development of many other categories of shells like:
⇒ Korn shell (ksh)
⇒ Almquist shell (ash)
⇒ Bourne-again shell (Bash)
.Ch 2 - Open up a Terminal
Windows comes with its own flavors of operating system shells such as the Command Prompt, the Powershell and the Windows Subsystem for Linux. Or you can always switch to a Unix style shell.
.To open up a shell :
Command Prompt: Press Win + R→ type cmd→ press Enter ↵
Powershell: Press Win + R → type powershell→ press Enter ↵
Unix Shells:
⇒ Install Git for windows ( recommended)
⇒ Install Windows sybsystem for Linux
⇒ Install a Unix/Linux Emulator or a Secure Shell (SSH)
.The macOS operating system includes the Terminal ( Terminal.app) as its default Terminal emulator. You can choose from a variety of shells such as Korn shell, tcsh, bash, zsh etc to use within your terminal emulator.
Zsh is the default Unix Shell for macOS Catalina or later releases.
For macOS Mojave or earlier releases, the default Unix Shell is Bash.
.Open up a Terminal via Finder:
⇒ In Finder→ select the Go menu→ select Utilities→ locate Terminal in the Utilities folder → open it.
⇒ In Finder→ select the Go menu→ use the macSpotlight→ search Terminal→ press return ↵.
.Which shell is your terminal emulator currently using ?
type echo $SHELL in the terminal window and press return ↵
.Some familiar terminal emulators found in Linux distros are Gnome Terminal, KDE Konsole and xterm.
The default Unix Shell for Linux operating systems is usually Bash.
.You can easily access the unix shell in Linux via the applications menu or the search bar by running the terminal emulator installed on the machine.
.Simple ways to open up a Terminal :
⇒ Press Ctrl + Alt + T→ opens a terminal window in some linux flavors.
⇒ Press Alt + F2→ type gnome-terminal→ press enter ↵.
.Check the calendar:
List files and directories:
Gather information about logged in users:
The following commands give the same output:
Ch3 - Printing Text
echo is the basic command for printing text in the terminal
It puts a new line at the end of every echo invocation by default.
.
.
side effect: echo without quotes wont let us use special characters like ( ! ) the exclamation.
.
Printing special characters such as ! @ # $ %can be done by either using quotes or my escaping them with a slash \ .
.
.
Echos can also accept escape sequences in double quoted strings as an argument. When using escape sequences use -e
.
.
printf takes quoted strings or arguments delimited by spaces. It does not have a new line as in the echo command.
.
.
%-5s : string substitution with left alignment with width = 5
5 is the number of characters reserved for the variable.
If – was not specified, the string would have been aligned to the right.
.
Format substitution characters
%s: string specifier for string output
%b: string specifier similar as %s ( allows us to interpret escape sequences with an argument )
%d: integer specifier for showing integer values
%f: floating point value specifier
%x: lowercase hexadecimal value specifier
.
Example:
.
You can also apply color and background color to the text that is being printed.
.
.
\e[1;31m : sets the color ( Replace 31 with the required color code )
\e[0m : resets the color
.
Color Codes:
.
Background color Codes:
.
Ch4 - Variables & Environment Variables
An Envirnoment variable is a dynamic-named value that is often initialized on system startup and is used by the shell environment and the operating system to store special values. It can affect the way a process behaves in a computer. A running process can make use of the data stored in an environment variable in various ways.
.They were introduced in their modern form in 1979 with Version 7 Unix. From PC DOS 2.0 in 1982, all succeeding Microsoft operating system, including Microsoft Windows and OS/2 also have included them as a feature although with a somewhat different syntax, usage and standard variable names.
.In Unix based systems, each process has its own set of environment variables. When a process is created, it inherits a duplicate environment of its parent process unless explicitly changed by the parent. An environment variable changed in a process, will only affect itself and the child processes.
.View all the environment variables related to a process or terminal:
Add a new path to the path environment:
Ch5 - Mathematics
To assign a value to a variable, we do not use spaces between the operand, operator and the value :
bc or Basic Calculator is a simple command line utility that allows you to perform scientific/financial calculations.
Interactive mode:
Non-Interactive mode:
Basic Movements
Title | Code |
---|---|
Move back one character | Ctrl + b |
Move forward one character | Ctrl + f |
Delete current character | Ctrl + d |
Delete previous character | Backspace |
Undo | Ctrl + _ |
Move to the start of line | Ctrl + a |
Move to the end of line | Ctrl + e |
Move forward a word | Meta or Alt + f |
Move backward a word | Meta or Alt + b |
Clear the screen | Ctrl + l or Command + k (on mac) |
Cut starting from the cursor to the end of line | Ctrl +k |
Cut starting from cursor to the end of word | Meta or Alt + d |
Cut starting from cursor to the start of word | Meta or Alt + Backspace |
Cut from cursor to previous whitespace | Ctrl + w |
Paste the last cut text | Ctrl + y |
Loop through and paste previously cut text | Meta + y (use it after Ctrl + y) |
Loop through and paste the last argument of previous commands | Meta + . |
Search as you type | (repeat)Ctrl + r and type the search term |
Search the last remembered search term | Ctrl + r twice |
End the search at current history entry | Ctrl + j |
Cancel the search and restore original line | Ctrl + g |
Meta is the Alt key.
.Enabling Meta key on a Mac
Open Terminal > Preferences > Settings > Keyboard and enable it.
Use option as meta key. Meta key, by convention, is used for operations on word.
.Ch6 - Files & File Descriptors
A Unix file system consists of ordinary files, special files and directories that are organized in a tree like structure where a single directory can hold multiple files and directories.
.Basic Navigation:
Goto home directory
Goto last directory
Move back one or more directories:
Print working directory
List contents within a directory
Changing directory
List files and directories
List files and directories
Metacharacter ( * ):
Use * to match 0 or more characters
Display hidden files :
Create a directory
Remove a directory
Create a file
Edit a file
View file content:
Count words:
Copy files:
Rename a file:
Delete a file:
Every file in Unix has permission attributes ( Owner permissions, Group permissions, Other permissions ). These permissions can be a combination of one or more modes – read, write and execute.
⇒ Read (r) ( View contents of a file )
⇒ Write (w) ( Modify or remove the contents of a file)
⇒ Execute (x) ( Run the file as a program )
.Changing Permissions:
The permissions of a file or a directory can be changed using the chmod ( change mode) command
⇒ + ( Add a permission )
⇒ – ( Remove a permission )
⇒ = ( Set a permission )
.User denotations:
⇒ u ( user/owner )
⇒ g ( group )
⇒ o ( other )
⇒ a ( all )
.Permission Types
(0) No permission: —
(1) Execute: –x
(2) Write: -w-
(3) Execute + Write: -wx
(4) Read: r–
(5) Read + Execute: r-x
(6) Read + Write: rw-
(7) Read + Write + Execute: rwx
.Example
Here, 764 means:
⇒ the owner can Read+Write+Execute
⇒ a user group can Read + Write
⇒ World can only read
Shown as : -rwxrw-r–
.Set other user permission:
Remove read permission for user:
Add execute permission for the user group:
Change ownership:
Change group owner only:
They are integers that are associated with an open file or data stream ( input and output). They keep track of open files.
.Reserved File descriptors:
0 – stdin ( standard input )
1 – stdout ( standard output )
2 – stderr ( standard error )
Writing to a file : (The content of the file will be truncated before writing)
Append to a file :
View the file contents :
Ch7 - Aliases
Create an Alias:
Aliases are temporary by default. They exist until we close the current terminal.
.To store the aliases permanently: add it to the ~/.bash_rc file. Commands in .bash_rc file are always executed whenever a new shell process is spawned.
.