5
5
or execute commands. Shells are key tools for users to manage the resources and processes of a
computer system. Shells are used for managing processes, providing access to system resources,
controlling the flow of data between programs, interpreting commands from users side.
There are two main types of shells:
* Command-line Shells - text-based interface for interacting with an operating system or a computer
system through a command-line interface(CLI). It allows users to enter
text commands, typically in the form of text strings, and receive text-based responses from the
system. Command-line shells are powerful tools for administration and development,
as they provide precise control over the system and can be used for various tasks, such as file
management, process control, system configuration, and automation.(ex Linux terminal)
Graphical User Interface(GUI) Shell - GUI shell provides a graphical interface for users to interact
with the operating system. Instead of typing commands, users interact with icons, windows, menus,
and buttons using a mouse or touch input.
Examples:Windows Explorer shell on Windows, the macOS Finder,Linux desktop environments
like GNOME.
Both GUI (Graphical User Interface) and CLI (Command-Line Interface) commands ultimately get
translated to operating system actions through a series of interactions involving various
software components and layers
regardless of the interface used, OS job is to manage hardware resources, execute system calls,
ensure that requested actions are carried out securely and efficiently.
Command lines are powerful tools in Linux; they’re the basis on which many of the friendlier GUI
tools are built. They can be accessed without the help of a GUI, they can be scripted
terminal - text-based interface that allows you to interact with your computer using text commands,
aka "command line interface" (CLI) or "shell." The terminal provides a way
for users to input commands and receive text-based responses from the operating system.
Terminals can be used to connect to remote servers and systems through protocols like SSH (Secure
Shell), allowing you to manage remote machines from your local terminal.
Aliases - In PowerShell, aliases are shortcuts or alternate names for cmdlets, functions,scripts.
Aliases allows users to use shorter and more familiar names for commands. PowerShell includes
several built-in aliases, and users can also create their own custom aliases.
Bash("Bourne-Again Shell") - widely used Unix shell(command-line interpreter), in the Unix and
Linux operating systems. It is the default shell for many Linux distributions and macOS.
Bash provides a command-line interface (CLI) for users to interact with the operating system by
entering text commands. widely used in scripting and automation tasks.
.sh file extension is commonly used to denote shell script files in Linux and other OS.
Shell scripts are plain text files containing a series of commands and instructions that are
executed by a shell interpreter, such as Bash (Bourne-Again Shell) or another compatible shell.
'echo' - command in Linux, used to print text or output to the terminal. It is one of the most
basic and frequently used commands in shell scripting.
'sleep' - command used to introduce a delay or pause in the execution of a shell script or command.
It allows you to specify the amount of time in seconds for which the script or command
should be paused before continuing execution. template: sleep time_in_seconds.
*sleep command is often used in shell scripts and automation tasks to introduce delays between
commands or to schedule tasks at specific intervals. It's a simple and effective way to control
the timing of script execution.
The "shebang" - also known as a "hashbang" or "pound-bang." is a special line of text that appears
at the very beginning of a script or executable file, specifically in Unix-like operating systems.
The shebang line is used to specify the interpreter that should be used to execute the
script or program. shebang template: #!/path/to/interpreter
*The shebang line is key for making a script executable without explicitly specifying the
interpreter each time you run it. It simplifies the execution process and ensures that the correct
interpreter is used, even if the user does not explicitly specify it.
*Exmaple: If Bash script starts with the shebang #!/bin/bash, you can run the script by simply
typing its filename, and the system will automatically use the Bash shell to execute it.
File permissions determine who can access a file or directory and what actions(read, write, execute)
they can perform on it. File permissions are represented by a set of characters, and they can be
viewed and modified using the 'ls -l' and 'chmod' commands. template: { -rwxrwxr-- ...
file_name.txt }
r (read) - Allows the user to view the contents of the file.
w (write) - Allows the user to modify the file's contents or delete it.
x (execute) - For regular files, allows the user to execute the file as a program.
For directories, it allows the user to access the directory and list its contents.
'chmod' - command in Linux is used to change the permissions (file mode) of files and directories.
It allows user to specify who can read, write, and execute a file or directory.
*Permission Types: r (read), w (write), x (execute)
*Permission Modes:
u (user) - Refers to the owner of the file or directory.
g (group) - Refers to the group associated with the file or directory.
o (others) - Refers to users who are neither the owner nor part of the group.
a (all) - Refers to all three categories (user, group, and others).
*Permission Symbols:
+ (plus) - Adds the specified permission.
- (minus) - Removes the specified permission.
= (equal) - Sets the specified permission and removes all others.
example: chmod +rx script.sh
After making it executable, you can run the script by specifying its filename ./file_name.txt
variables - used to store data and information that can be accessed and manipulated within a
shell session. Shells like bash, allow you to work with variables to store and manage data,
which can be text strings, numbers, or any other type of information.
*You can assign a value to a variable using the = operator with no spaces around it.
example: user1='timmy' year=2023
*To access the value stored in a variable, you can use the $ symbol followed by the variable name
example: echo ' hello $user1, current year is $year
*Unsetting Variables - unset (remove) a variable using the unset command
*Environment Variables - global variables that can be accessed by processes spawned from the
shell.
They are often used to store system-wide configuration and information. Common environment
variables:
PATH (specifies where executable files are located),HOME (points to the user's home directory).
*Local Variables - specific to the current shell session or script and are not accessible to other
processes or scripts. They are typically created and used within a shell script or function.
*Special Variables - Shells also provide a set of special variables with predefined meanings.
ex: $0 - the name of the script or shell, $1, $2, etc., command-line arguments, $? the exit status
of the last executed command.
*To capture the output of a command and store it in a variable in a Bash script, you can use
command substitution by using $(). example: user=$(whoami)
arguments - Shell arguments, or command-line arguments or positional parameters, are values that
are
passed to a shell script or a command when it is executed in a terminal or shell. These arguments
provide a way to pass input data or options to a script or command, allowing them to be more
flexible
and customizable. Shell scripts and commands can access and process arguments to perform
specific tasks
exported variables are available to child processes and scripts but do not affect the parent shell's
environment. To make an environment variable permanent and available across sessions, add the
export
command to your shell's startup files (e.g., ~/.bashrc, ~/.bash_profile,..) to ensure it's set
every time you start a new shell session.
.bashrc file - script that's executed whenever you start an interactive Bash shell session. It is
located in your home directory, and its purpose is to set up your shell environment by defining
various settings and customizations. Users can define custom environment variables in .bashrc
In Bash, you can perform arithmetic operations using variables by using the $((...)) construct.
This construct allows you to evaluate arithmetic expressions and store the result in a variable.
*$((...)) construct is used for integer arithmetic, it cant perform floating-point calculations
ex: num1=$(( 15 * 23 ))
RANDOM variable is a built-in environment variable that generates a random integer between
[0 and 32,767] each time it is referenced. You can use this variable to generate random
numbers in your shell scripts
'>' operator - used to redirect the standard output (stdout) of a command to a file.If the file
specified does not exist, it will be created. If it already exists, its content will be overwritten.
'>>' operator - used to append the standard output (stdout) of a command to a file. If the file
specified does not exist, it will be created. If it already exists, the new output will be added
to the end of the file, preserving the existing content.
In Linux, the pipe (|) is an operator used to connect the standard output of one command to the
standard input of another command. Allows users to create a chain of commands where the output
of
one command serves as the input to the next command. This is known as "piping" commands
together.
template: command1 | command2 | command3 | ...
grep(Global Regular Expression Print) - command-line utility in Linux that is used for searching
text or regular expressions in files. grep allows you to search for patterns within one or more
files or to search for text piped from another command. It is commonly used for text processing
and data extraction tasks.
template1: grep "pattern" filename.txt; template2: some_command | grep "pattern"