Fandom

How To Wiki

How to read command line arguments in a bash script

1,795pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

To input arguments into a Bash script, like any normal command line program, there are special variables set aside for this

The arguments are stored in variables with a number in the order of the argument starting at 1

  • First Argument: $1
  • Second Argument: $2
  • Third Argument: $3
  • Example
    • command: ./script.bash alpha beta gamma
    • Variables: $1=='alpha'; $2=='beta'; $3=='gamma'


The variable $0 is the script's name. The total number of arguments is stored in $#. The variables $@ and $* return all the arguments.

For more complicated examples, you might consider getopt: http://aplawrence.com/Unix/getopts.html

Script ExampleEdit

#!/bin/bash
echo "the $1 eats a $2 every time there is a $3"
echo "bye:-)"
  • Command: ./script.bash dog bone moose
  • Output:
    the dog eats a bone every time there is a moose
    bye:-)

See AlsoEdit

Also on Fandom

Random Wiki