Below are some of the Special Variables in Shell scripting:
1] $0 - Display the name of the current script.
2] $1..$n - Display the arguments passed $n referes to the position.
EX: If we pass below arguments:
Shell Script for the world
$1(First arg) - Shell
$2(Second arg) - Script
$3(Third arg) - for
$4(Fourth arg) - the
$5(Fifth arg) - world
3] $# - Total number of arguments passed.
4] $* - Display all the arguments.
5] $@ - Display all the arguments.
Note the difference between $* and $@:
If written without double quotes ("") - Both will simply display the args in single line seperated by space
If $* Written within double quotes("$*") - Takes all the args as a single arg and display in single line
If $@ Written within double quotes("$@") - Display each arg with line break.
6] $? - Exit status of the last command.
7] $$ - Process id of the current shell.
STEP 1: Open a text file, write the below commands and save it as test.sh .
#!/bin/bash
echo "special variables"
echo "current script name : "$0
echo "first argument : "$1
echo "second argument: "$2
echo "total no of arguments Passed : "$#
echo "all the arguments are displayed : "$*
echo "all the arguments are displayed : "$@
echo "the exit status of the last command : "$?
echo " IF EXIT STATUS IS ZERO COMMAND WAS SUCCESS FULL"
echo " IF EXIT STATUS IS ONE COMMAND WAS UNSUCCESS FULL"
echo "process number or id of the currrent shell : "$$
STEP 2: Copy it to a folder in your unix machine.
STEP 3 : Navigate to the folder : cd /apps/test/
STEP 4 : Run with command : ./test.sh
No args are passed.
STEP 5 : Run command with arguments : ./test.sh shell script for world
Args passed are: shell script for world (see explanation above)
Check out below link for creating and running your first shell script.
https://techeateassy.blogspot.com/2018/01/shell-scripting-1-create-and-run-your.html
1] $0 - Display the name of the current script.
2] $1..$n - Display the arguments passed $n referes to the position.
EX: If we pass below arguments:
Shell Script for the world
$1(First arg) - Shell
$2(Second arg) - Script
$3(Third arg) - for
$4(Fourth arg) - the
$5(Fifth arg) - world
3] $# - Total number of arguments passed.
4] $* - Display all the arguments.
5] $@ - Display all the arguments.
Note the difference between $* and $@:
If written without double quotes ("") - Both will simply display the args in single line seperated by space
If $* Written within double quotes("$*") - Takes all the args as a single arg and display in single line
If $@ Written within double quotes("$@") - Display each arg with line break.
6] $? - Exit status of the last command.
7] $$ - Process id of the current shell.
STEP 1: Open a text file, write the below commands and save it as test.sh .
#!/bin/bash
echo "special variables"
echo "current script name : "$0
echo "first argument : "$1
echo "second argument: "$2
echo "total no of arguments Passed : "$#
echo "all the arguments are displayed : "$*
echo "all the arguments are displayed : "$@
echo "the exit status of the last command : "$?
echo " IF EXIT STATUS IS ZERO COMMAND WAS SUCCESS FULL"
echo " IF EXIT STATUS IS ONE COMMAND WAS UNSUCCESS FULL"
echo "process number or id of the currrent shell : "$$
STEP 2: Copy it to a folder in your unix machine.
Add caption |
STEP 3 : Navigate to the folder : cd /apps/test/
STEP 4 : Run with command : ./test.sh
No args are passed.
STEP 5 : Run command with arguments : ./test.sh shell script for world
Args passed are: shell script for world (see explanation above)
https://techeateassy.blogspot.com/2018/01/shell-scripting-1-create-and-run-your.html
No comments:
Post a Comment