Shell program is series of Linux commands. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. Shell script can take input from user, file and output them on screen. Useful to create our own commands that can save our lots of time and to automate some task of day today life.
Variables in Linux
Sometimes to process our data/information, it must be kept in computers RAM memory. RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable (Its a named storage location that may take different values, but only one at a time).
Also Read:
In Linux, there are two types of variable:
- System variables – Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
- User defined variable (UDV) – Created and maintained by user. This type of variable defined in lower LETTERS.
Predefined Variables
Like most shells, Bash defines some variables when it starts-
S.No. | Name | Value |
---|---|---|
1 | $- | The current shell options assigned from the command line or by the built-in set command discussed later. |
2 | $$ | The process ID of this shell. |
3 | $! | The process ID of the last background command. |
4 | $# | The number of positional parameters. |
5 | $? | The exit value of the last command. |
6 | $@ | An individually quoted list of all the positional parameters. |
7 | $_ | The last parameter of the previous command. |
8 | $BASH | The full pathname of the Bash executable. |
9 | $BASH_ENV | Location of Bash’s startup file (default is ~/.bashrc). |
10 | $BASH_VERSINFO | A read-only array of version information. |
11 | $BASH_VERSION | Version string. |
12 | $DIRSTACK | Array defining the directory stack (discussed later). |
13 | $ENV | If this variable is not set, the shell searches the user’s home directory for the “.profile” startup file when a new login shell is created. If this variable is set, then every new shell invocation runs the script specified by ENV. |
14 | $EUID | Read-only value of effective user ID of user running Bash. |
15 | $HISTFILE | Location of file containing shell history (default ~/.bash_history). |
16 | $HISTFILESIZE | Maximum number of lines allowed in history file (default is 500). |
17 | $HISTSIZE | Maximum number of commands in history (default is 500). |
18 | $HOSTNAME | Hostname of machine where Bash is running. |
19 | $HOSTTYPE | Type of host where Bash in running. |
20 | $IFS | When the shell tokenizes a command line prior to its execution, it uses the characters in this variable as delimiters. IFS usually contains a space, a tab, and a newline character. |
21 | $LINES | Used by select to determine how to display the selections. |
22 | $MAILCHECK | How often (seconds) to check for new mail. |
23 | $OLDPWD | The previous working directory of the shell. |
24 | $OSTYPE | Operating system of machine where Bash is running. |
25 | $PPID | The process ID number of the shell’s parent. |
26 | $PPID | Read-only process ID of the parent process of Bash. |
27 | $PS1 | This contains the value of the command-line prompt, and is $ by default. To change the command-line prompt, simply set PS1 to a new value. |
28 | $PS2 | This contains the value of the secondary command-line prompt that is displayed when more input is required by the shell, and is > by default. To change the prompt, set PS2 to a new value. |
29 | $PS3 | The prompt used by the select command, #? by default. |
30 | $PWD | The current working directory of the shell. |
31 | $RANDOM | A random integer. |
32 | $REPLY | Set by a select command. |
33 | $SHLVL | Level of shell (incremented once each time a Bash process is started, it shows how deeply the shell is nested). |
34 | $UID | Read-only value of user ID of user running Bash. |
How to define User defined variables (UDV)
To define UDV use following syntax
Syntax: variablename=value
NOTE: Here ‘value’ is assigned to given ‘variablename’ and Value must be on right side = sign For
e.g.
$no=20 # this is OK
$20=no # Error, NOT Ok, Value must be on right side of = sign.
To define variable called ‘vari’ having value Bus
$vari=CAR
To define variable called n having value 20
$n=20
Rules for naming variable name (Both UDV and System Variable)
- Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character, For e.g. Valid shell variable are as follows HOME
- Don’t put spaces on either side of the equal sign when assigning value to variable.
- Variables are case-sensitive, just like filename in Linux.
- You can also define NULL variable (NULL variable is variable which has no value at the time of definition) For e.g.
- Do not use ?,* etc, to name your variable names.
Metacharacters
Some characters are processed specially by the shell, and are known as metacharacters. All shells share a core set of common metacharacters.
S.No. | Name | Value |
---|---|---|
1 | > | Output redirection; writes standard output to a file. |
2 | >> | Output redirection; appends standard output to a file. |
3 | < | Input redirection; reads standard input from a file. |
4 | * | File substitution wildcard; matches zero or more characters. |
5 | ? | File substitution wildcard; matches any single character. |
6 | […] | File substitution wildcard; matches any character between backets. |
7 | ` command ` | Command substitution; replaced by the output from command. |
8 | | | Pipe symbol; sends the output of one process to the input of another. |
9 | ; | Used to sequence commands. |
10 | || | Conditional execution; executes a command if the previous one failed. |
11 | && | Conditional execution; executes a command if the previous one succeeded. |
12 | (…) | Groups commands. |
13 | & | Runs a command in the background. |
14 | # | All characters that follow up to a newline are ignored by the shell and programs (i.e., a comment). |
15 | $ | Expands the value of a variable. |
16 | \ | Prevents special interpretation of the next character. |
17 | <<tok | Input redirection; reads standard input from script up to tok. |
When you enter a command, the shell scans it for metacharacters and processes them specially. When all metacharacters have been processed, the command is finally executed. To turn off the special meaning of a metacharacter, precede it by a \ character.
You may also like:- Top 40 Useful Linux Command Snippets
- Essential Commands For Process Management in Kali Linux
- A Comprehensive Guide to File System Commands in Linux
- Essential File Compression Commands in Linux
- Secure Shell (SSH) Protocol – A Comprehensive Guide
- Monitoring Active Connections in Kali Linux Using Netstat
- Manage Time and Date in Linux with timedatectl
- How to Add a User to Sudoers on Ubuntu
- 25 Popular Linux IP Command Examples
- Top 11 Nmap Commands for Remote Host Scanning