AMP AMP

A view of Special Variables in Shell Scripting

A view of Special Variables in Shell Scripting

Introduction:

A shell script is a listing of commands in a computer program that runs by the Unix shell which is a command-line interpreter. The most important special, built-in variables are called positional parameters. These hold the command-line arguments to scripts when they are invoked. Positional parameters have the names 1, 2, 3, etc., meaning that their values are denoted by $1, $2, $3, etc.

Check the OS version by using the below command:

[root@linuxhelp ~]# lsb_release -d
Description:	CentOS Linux release 7.6.1810 (Core) 

Create a special variable file:

[root@linuxhelp ~]# vim special_var.sh

snap1

Execute the special_var script file:

[root@linuxhelp ~]# sh special_var.sh
The output of '' is 
The output of '0' is 0
' and ' output is  and 
the output of '' is 
The output of '0' is 0
The output of '12366' is 12366
The output of '' is 
The output of 'special_var.sh' is your file name = special_var.sh

Show the content of the file:

[root@linuxhelp ~]# cat special_var.sh
#!/bin/bash
echo "The output of '$*' is $*"
echo "The output of '$#' is $#"
echo "'$1 and $2' output is $1 and $2"
echo "the output of '$@' is $@"
echo "The output of '$?' is $?"
echo "The output of '$$' is $$"
echo "The output of '$!' is $!"
echo "The output of '$0' is your file name = $0"

Execute the file with command line arguments:

[root@linuxhelp ~]# sh special_var.sh welcome to linuxhelp
The output of 'welcome to linuxhelp' is welcome to linuxhelp
The output of '3' is 3
'welcome and to' output is welcome and to
the output of 'welcome to linuxhelp' is welcome to linuxhelp
The output of '0' is 0
The output of '12397' is 12397
The output of '' is 
The output of 'special_var.sh' is your file name = special_var.sh

With this method a view of special variables in Shell Scripting comes to an end.

FAQ
Q
How do I permanently add to my path?
A
To make the change permanent, enter the command PATH=$PATH:/opt/bin into your home directories. bashrc file. When you do this, you're creating a new PATH variable by appending a directory to the current PATH variable, $PATH.
Q
How do you write a command-line argument in a shell script?
A
Simply list the arguments on the command line when running a shell script. In the shell script, $0 is the name of the command run (usually the name of the shell script file); $1 is the first argument, $2 is the second argument, $3 is the third argument, etc...
Q
What is $? In shell script?
A
$? -The exit status of the last command executed. $0 -The filename of the current script. $# -The number of arguments supplied to a script. ... For shell scripts, this is the process ID under which they are executing.
Q
What are $1 and $2 in a shell script?
A
$1 is the first command-line argument passed to the shell script. ... $0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1).
Q
What are the special shell variables in the script language?
A
The most important special, built-in variables are called positional parameters. These hold the command-line arguments to scripts when they are invoked. Positional parameters have the names 1, 2, 3, etc., meaning that their values are denoted by $1, $2, $3, etc.