• 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

Command line parameters in shell scripts

by admin

Command line parameters are a way to pass information into a program or script in order for it to do what you want it to. Some examples of command line parameters:

$ ls -l
$ cat textfile

The command line parameters here are the “-l” and “textfile”.

How are command line parameters accessed within a shell script? They are stored in these variables:

  • “$0”: This holds the name of the command.
  • “$1”: This holds the first parameter.
  • “$2”: This holds the second parameter.
  • “$3”: This holds the third parameter and the pattern repeats.
  • “$#”: This holds the number of parameters that have been passed.
  • “$@”: This holds all of the parameters

Lets make a simple script call it parameters.sh:

#!/bin/sh
echo "Name of script: $0"
echo "First parameter: $1"
echo "Second parameter: $2"
echo "Number of parameters: $#"
echo "All parameters: $@"

When we run the script you should get the following output
Input:

$ sh parameters.sh hello world

Output:

Name of script: parameters.sh
First parameter: hello
Second parameter: world
Number of parameters: 2
All parameters: hello world

Filed Under: Linux

Some more articles you might also be interested in …

  1. Linux OS service ‘nfs’
  2. How to Create and Query a BTRFS File System
  3. csplit Command Examples in Linux
  4. Understanding the /etc/inittab File in Linux
  5. auracle Command Examples
  6. How to enable additional scsi logging in CentOS/RHEL
  7. findfs Command Examples in Linux
  8. How To Check a Disk for Bad Blocks or Disk Errors on CentOS / RHEL
  9. Swapon Fails To Mount Swap – Gives Invalid Argument Error
  10. lpq: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright