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
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.
Comments ( 0 )
No comments available