• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Understanding Positional Parameters (Passing Parameters/Arguments to Shell script)

by admin

The execution of scripts is made versatile by the ability to run a script based on arguments supplied on the command line. In this way, the operation of the script varies depending on what arguments are given to the script. The shell automatically assigns special variable names, called positional parameters, to each argument supplied to a script on the command line. The positional parameter names and meanings are shown in Table below.

Positional Parameter Name Returns true (0) if:
$0 The name of the script
$1 The first argument to the script
$2 The second argument to the script
$n The n-th argument to the script
$# The number of arguments to the script
$@ A list of all arguments to the script
$* A list of all arguments to the script
${#N} The length of the value of positional parameter N (Korn shell only)

$@ and $* have the same meaning. This is true if they are not enclosed in double quotes ” “.

Note – The Bourne shell stores all the arguments passed to the script, but can refer to only the positional parameters $1 through $9. To obtain the values beyond the ninth argument, use the shift statement, which is destructive in nature.

Using if loop to Check Command-Line Arguments

The following script captures the command-line arguments in special variables: $#, $1, $2, $3, and so on. The $# variable captures the number of command-line arguments. The following example use the command line arguments to do a comparison.

# cat arg.sh
#!/bin/bash

if [ $1 -gt $2 ] 
then
	echo "num1 is larger"
else
	echo "num2 is larger"
fi

You can run the above script as shown below.

$ ./a 23 45
num2 is larger
Bash if loop examples (if then fi, if then elif fi, if then else fi)
Bash for loop Examples

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. How to use “break” and “continue” statements in shell scripts
  2. How to Configure Network Namespaces in Docker Containers
  3. How to debug shell scripts
  4. How to Configure Btrfs as the Storage Engine in Docker
  5. My Development Environment Set up on Windows to use Python for Web Dev & Data Science
  6. How to Trace Python Scripts using trace.py
  7. Bash for loop Examples
  8. Introduction to sed (Stream Editor) : Useful sed Command Examples
  9. ValueError: Masked arrays must be 1-D
  10. How to List / Search / Pull docker images on Linux

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright