Created: 2022/12/17
What is Shell? | |||||||||||||||||
A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions. |
|||||||||||||||||
Shell Prompt |
$ // Bourne shell % // C shell The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command. Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words. |
||||||||||||||||
Shell Types |
|
||||||||||||||||
Shell Scripts |
suffix: .sh
|
||||||||||||||||
Using Variables | |||||||||||||||||
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. |
|||||||||||||||||
Variable Names |
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _). By convention, Unix shell variables will have their names in UPPERCASE. |
||||||||||||||||
Defining Variables |
variable_name=variable_value
|
||||||||||||||||
Accessing Values |
To access the value stored in a variable, prefix its name with the dollar sign ($) TEST=ture echo $TEST
|
||||||||||||||||
Read-only Variables |
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed. TEST=true readonly TEST
or readonly TEST=true
|
||||||||||||||||
Unsetting Variables |
unset variable_name
|
||||||||||||||||
Variable Types |
|
||||||||||||||||
Special Variables | |||||||||||||||||
|
|||||||||||||||||
Command-Line Arguments |
The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command. Following script uses various special variables related to the command line − #!/bin/sh echo "File Name: $0" echo "First Parameter : $1" echo "Second Parameter : $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters : $#" output: $./test.sh Zara Ali File Name : ./test.sh First Parameter : Zara Second Parameter : Ali Quoted Values: Zara Ali Quoted Values: Zara Ali Total Number of Parameters : 2
|
||||||||||||||||
Special Parameters $* and $@ |
There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "". Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into separate arguments. |
||||||||||||||||
Exit Status | |||||||||||||||||
Using Arrays | |||||||||||||||||
Basic Operators | |||||||||||||||||
Decision Making | |||||||||||||||||
Shell Loops | |||||||||||||||||
Shell Substitutions | |||||||||||||||||
Quoting Mechanisms | |||||||||||||||||
IO Redirections | |||||||||||||||||
Shell Functions | |||||||||||||||||
Manpage Help | |||||||||||||||||