InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Using Parameters in a Shell Script in bash
Bash

Using Parameters in a Shell Script in bash

InfinityCoder February 14, 2017

You also want users to be able to invoke your script with a parameter. You could require that users set a shell variable, but that seems clunky.

You also need to pass data to another script.

You could agree on environment variables, but that ties the two scripts together too closely.

Use command-line parameters.

Any words put on the command line of a shell script are available to the script as numbered variables:

1
2
# simple shell script
echo $1

The script will echo the first parameter supplied on the command line when it is invoked.

Here it is in action:

1
2
3
4
5
6
7
8
$ cat simplest.sh
# simple shell script
echo ${1}
$ ./simplest.sh you see what I mean
you
$ ./simplest.sh one more time
one
$

The other parameters are available as ${2}, ${3}, ${4}, ${5}, and so on.

You don’t need the braces for the single-digit numbers, except to separate the variable name from the surrounding text.

Typical scripts have only a handful of parameters, but when you get to ${10} you better use the braces or else the shell will interpret that as ${1} followed immediately by the literal string 0 as we see here:

1
2
3
4
5
$ cat tricky.sh
echo $1 $10 ${10}
$ ./tricky.sh I II III IV V VI VII VIII IX X XI
I I0 X
$

The tenth argument has the value X but if you write $10 in your script, then the shell will give you $1, the first parameter, followed immediately by a zero, the literal character that you put next to the $1 in your echo statement.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Searching with More Complex Patterns in bash
The regular expression mechanism of grep provides for some very …

Searching with More Complex Patterns in bash

Giving an Error Message for Unset Parameters in bash
Those shorthands for giving a default value are cool, but …

Giving an Error Message for Unset Parameters in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap