0% found this document useful (0 votes)
29 views11 pages

Unit - 3

It is not working on the way to study the toh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Unit - 3

It is not working on the way to study the toh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT 3 – SHELL BASICS

Meaning and Purpose of Shell :


In Linux, the shell is a command-line interface (CLI) that provides a user-friendly
way to interact with the operating system. It is a program that reads user input
from the keyboard or a script, interprets it, and then executes commands on behalf
of the user. The shell acts as an intermediary between the user and the operating
system, allowing the user to execute commands, run scripts, and perform various
other tasks.
The shell provides a way for users to interact with the system, allowing them to
manage files, run programs, and manipulate data. It also provides a way for users
to customize their environment, defining variables, setting aliases, and creating
functions to automate tasks. Overall, the purpose of the shell is to provide an
efficient and flexible way for users to interact with the Linux operating system.

Introduction to Types of Shell :

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


1. Bourne-Again Shell

Bash stands for Bourne Again Shell and it is the default shell on many Linux
distributions today. It is also a sh-compatible shell and offers practical
improvements over sh for programming and interactive use which includes:

 Command line editing


 Job Control
 Unlimited size command history
 Shell Functions and Aliases
 Unlimited size Indexed arrays
 Integer arithmetic in any base from two to sixty-four

2. TENEX C Shell

Tcsh is enhanced C shell, it can be used as an interactive login shell and shell
script command processor.

Tcsh has the following features:

 C like syntax
 Command-line editor
 Programmable word and filename completion
 Spelling correction
 Job control

3. Korn Shell

Ksh stands for Korn shell and was designed and developed by David G. Korn.
It is a complete, powerful, high-level programming language and also an
interactive command language just like many other Unix/GNU Linux shells.

The Korn shell includes features from other shells and provides several more
advanced features found in modern scripting languages such as;

 associative arrays
 floating point arithmetic
 job control
 command aliasing
 command history
 supports POSIX standards
 backward compatibility with bash

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


4. Z Shell

Zsh is designed to be interactive and it incorporates many features of other


Unix/GNU Linux shells such as bash, tcsh, and ksh.

It is also a powerful scripting language just like the other shells available. Though
it has some unique features that include:

 Filename generation
 Startup files
 Login/Logout watching
 Closing comments
 Concept index
 Variable index
 Functions index
 Key index and many more that you can find out in man pages

5. Scheme Shell

The Scheme shell (scsh) is an exotic shell that offers a scripting environment
using Scheme, which is a derivative of the Lisp language. The Pyshell is an
attempt to create a similar script that uses the Python language.

Check all available shells :

cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
To check current shell use echo $SHELL

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


The Command Line :
In Linux, the command line is a text-based interface that allows users to interact
with the operating system by typing commands. The command line consists of a
prompt, which is typically a string of characters indicating the current directory
and the user's name or ID, followed by a command and any options or arguments.
Here is an example of a command line prompt:
user@computer:~$
In this example, the prompt includes the username ("user"), the hostname of the
computer ("computer"), and the current directory ("~", which is shorthand for the
user's home directory). The "$" symbol indicates that the shell is ready to accept
a command.
The command line is accessed through a program called the shell.
Using the command line, users can perform a variety of tasks such as navigating
the file system, creating, modifying, and deleting files and directories, installing
and managing software packages, configuring the system, and more.
The command line also supports various features such as command history, tab
completion, wildcard expansion, and piping.
 Command history allows users to recall previously executed commands and
reuse them, either by using the up and down arrow keys or the "history"
command.
 Tab completion helps users save time by automatically completing the
command or file name when they start typing it and pressing the "Tab" key.
 Wildcard expansion is a feature that allows users to match and operate on
multiple files at once using special characters like "*", "?" or "[]" in their
commands.
 Piping is a powerful feature that allows users to send the output of one
command as input to another command, enabling the creation of more
complex and customized commands.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Standard Input And Standard Output :
In Linux and Unix-like operating systems, standard input (stdin), standard
output (stdout), and standard error (stderr) are three standard communication
channels that are used by various command-line utilities and processes.
By default, when a command is executed in the terminal, it takes input from
standard input (stdin), processes it, and sends the output to standard output
(stdout). If there are any errors or warnings, they are sent to standard error
(stderr).
Here's how these channels work:
Standard Input (stdin): This is the default channel from where a program reads
input. It can be a file, keyboard, or output of another command. In most cases,
stdin is represented by the keyboard, and we type commands or data into it.
Standard Output (stdout): This is the default channel where a program writes its
output. It can be displayed on the terminal or redirected to a file.
Standard Error (stderr): This is the default channel where a program writes its
error messages or warnings. It is also displayed on the terminal by default.
In Linux, these standard channels are represented by file descriptors:
 stdin: File descriptor 0 (zero)
 stdout: File descriptor 1 (one)
 stderr: File descriptor 2 (two)
We can redirect the input and output of a command using redirection operators.
For example, the ">" operator is used to redirect stdout to a file, and the "2>"
operator is used to redirect stderr to a file. The "<" operator is used to redirect
input from a file or command.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Redirection :
Redirection is the process of changing the input or output of a command in Linux. In
Linux, input can come from different sources like the keyboard or a file, while the
output can go to different destinations like the terminal, a file, or another command.
Redirection is used to control the input and output sources and destinations of a
command.
There are three basic types of redirection in Linux:
1. Standard input (stdin): It is used to provide input to a command. By default, it
takes input from the keyboard, but it can also take input from a file or another
command using the redirection operator "<".
Syntax: command < input_file
Example: sort < names.txt

2. Standard output (stdout): It is used to redirect the output of a command to a file or


another command. By default, it sends output to the terminal, but it can also send
output to a file or another command using the redirection operator ">".
Syntax: command > output_file
Example: ls > filelist.txt
3. Pipes: It is used to redirect the output of one command to the input of another
command. It allows the chaining of multiple commands together, where the output
of one command becomes the input of the next command.
Syntax: command1 | command2
Example: ls | sort
Pipes :
In Linux, a pipe is a form of redirection used to send the output of one command to
another command for further processing. It allows two or more commands to be chained
together, with the output of one command being passed as input to the next command.
The syntax for using pipes in Linux is as follows:
command1 | command2
In this syntax, the vertical bar or the pipe symbol | is used to connect the two commands.
The output of command1 is sent to command2 as input, and command2 processes the
input and produces output.
Here is an example of using pipes in Linux:
ls -l | grep ".txt"
In this example, the ls command lists the contents of the current directory in long
format, and the output is piped to the grep command. The grep command searches for
lines that contain the string ".txt" and outputs those lines to the terminal.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Filters :
In Linux, filters are commands that take input from standard input, process it, and
then produce output to standard output. They are used for text manipulation, data
processing, and output formatting. Filters are often used in combination with
pipes to perform complex data manipulation tasks.
Here are some commonly used filters in Linux:
1. grep: grep is a tool used to search for specific patterns in a file or input stream.
It can search for text patterns using regular expressions and can be used with
pipes to filter input.
2. sed: sed is a stream editor that is used to perform basic text transformations on
an input stream. It can be used to replace text, delete lines, and perform other
text manipulations.
3. awk: awk is a scripting language that is used to perform text processing and
data extraction. It can be used to filter and transform data, and to generate
formatted reports.
4. cut: cut is a command used to extract specific fields or columns from a file or
input stream. It can be used to filter out unwanted information from a file.
5. sort: sort is a command used to sort the lines of a file or input stream. It can
be used to sort data in ascending or descending order based on specific fields
or columns.
6. uniq: uniq is a command used to remove duplicate lines from a file or input
stream. It can be used with sort to find unique lines in a file.
7. tr: tr is a command used to translate or delete characters in a file or input
stream. It can be used to filter out unwanted characters or to replace characters
with other characters.
8. wc: wc is a command used to count the number of lines, words, and characters
in a file or input stream. It can be used to filter out unwanted information and
to generate reports.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Special Characters for Searching Files and Pathnames:
In Linux, special characters are used in various ways to search files and
pathnames. Some of the commonly used special characters for searching files and
pathnames are:
1. Asterisk (*): This character is used to match any number of characters
(including zero) in a filename or pathname. For example, to list all files with
a .txt extension in the current directory, you can use the command:
ls *.txt
2. Question mark (?): This character is used to match exactly one character in a
filename or pathname. For example, to list all files with a three-letter name
and a .txt extension in the current directory, you can use the command:
ls ???.txt
3. Square brackets ([]): This character is used to match any one character from a
specified range of characters in a filename or pathname. For example, to list
all files that have either a "1" or "2" as the second character in the name and a
.txt extension in the current directory, you can use the command:
ls [12]?.txt
4. Tilde (~): This character is used as a shortcut to refer to the current user's home
directory. For example, to change to the current user's home directory, you
can use the command:
cd ~
5. Dot (.) and double dot (..): These characters are used to refer to the current
directory and the parent directory, respectively. For example, to change to the
parent directory of the current directory, you can use the command:
cd ..
6. Backslash (\): This character is used to escape special characters and treat
them as literal characters. For example, to search for a file that has an asterisk
(*) in its name, you can use the command:
ls \*.*
These special characters can be used with various commands in Linux, such as
ls, find, grep, and many others, to search for files and pathnames that match a
certain pattern or criteria.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Command Separation and Grouping :
In Linux, command separation and grouping refers to the techniques used to
execute multiple commands in a single line. This is useful when you want to
perform a series of related tasks or when you want to use the output of one
command as the input to another.
There are several ways to separate or group commands in Linux, including:
1. Semicolon (;) operator: The semicolon operator allows you to separate
commands and execute them in sequence. For example, to list the contents of
two directories and print the current working directory, you can use the
following command:
ls /etc; ls /usr/bin; pwd

2. Ampersand (&) operator: The ampersand operator allows you to run


commands in the background. For example, to start a program and continue
working in the shell, you can use the following command:
firefox &

3. Double ampersand (&&) operator: The double ampersand operator allows you
to execute the second command only if the first command succeeds. For
example, to compile and run a C program, you can use the following
command:
gcc -o myprog myprog.c && ./myprog

4. Double vertical bar (||) : The double vertical bar operator allows you to execute
the second command only if the first command fails. For example, to check if
a file exists and create it if it does not, you can use the following command:
[ -e myfile ] || touch myfile

5. Parentheses: Parentheses can be used to group commands and execute them


as a single unit. For example, to change to a directory, list its contents, and
change back to the original directory, you can use the following command:
( cd /etc; ls; cd -)

6. Braces: Braces can be used to group commands and execute them in the
current shell environment. For example, to create a variable and set its value,
you can use the following command:
{ var ="hello world" ; echo $var; }

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Directory Stack Manipulation :
In Linux, the directory stack is a list of directories that are stored in a special shell
variable called the directory stack. The directory stack is commonly used to keep
track of the directories visited by the user during a shell session. The user can
manipulate the directory stack using the following commands:
1. pushd: This command adds a directory to the top of the directory stack and
changes the current working directory to the specified directory. The syntax
for the pushd command is:
pushd [directory]
2. popd: This command removes the top directory from the directory stack and
changes the current working directory to the new top directory on the stack.
The syntax for the popd command is:
popd
3. dirs: This command displays the current contents of the directory stack. The
syntax for the dirs command is: dirs
4. dirs -v: This command displays the current contents of the directory stack,
along with their index numbers. The syntax for the dirs -v command is:
dirs -v
5. dirs +n: This command removes the directory at position n from the
directory stack. The syntax for the dirs +n command is:
dirs +n
6. dirs -n: This command removes the directory at position n from the
directory stack, counting from the right. The syntax for the dirs -n command
is:
dirs -n
7. dirs -c: This command clears the directory stack. The syntax for the dirs -c
command is:
dirs -c

By using these commands, users can easily manage the directory stack and
navigate through their directory history.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT


Processes :
In Linux, a process is a running instance of a program. When a program is executed,
the operating system creates a process for it. Each process is assigned a unique Process
ID (PID) and has its own address space and system resources.
Processes can run in the background or foreground. Foreground processes are those that
are executed in the terminal and require user input. Background processes are those that
run without user input and continue to execute even after the user has closed the
terminal.
The Linux operating system provides several commands to manage processes,
including:

1. ps: The ps command is used to list all the processes running on the system. It
displays information such as the PID, process status, CPU usage, and memory usage.
2. top: The top command is used to monitor the system in real-time. It displays the
processes that are using the most CPU and memory.
3. kill: The kill command is used to send a signal to a process to terminate it. The
default signal sent by the kill command is SIGTERM, which allows the process to
perform cleanup operations before it terminates. The SIGKILL signal can also be
used to terminate a process immediately.
4. pkill: The pkill command is used to kill processes based on their name or other
attributes. It is useful when you want to kill all the processes associated with a
particular program.
5. killall: The killall command is similar to pkill, but it matches the name of the process
exactly.
6. nice: The nice command is used to set the priority of a process. A higher nice value
means lower priority, and a lower nice value means higher priority.
7. renice: The renice command is used to change the priority of a running process.
8. htop: The htop command is similar to top, but it provides a more user-friendly
interface.
9. pstree: The pstree command is used to display the processes in a tree-like format,
showing the parent-child relationships between processes.
10. jobs: The jobs command is used to display the jobs running in the current shell
session.
11. bg: This command moves a currently suspended job to the background, allowing
other jobs to run in the foreground.
12. fg: This command brings a background job back to the foreground, allowing you to
interact with it again.
13. strace: This command allows you to trace system calls and signals made by a
running process, helping you to debug problems or understand how a program is
working.

MADE WITH AND HARDWORK BY VIKAS KUMAWAT

You might also like