A Simple Example of a Positional Parameter
For example, if a shell program mypgm
expects two parameters—such as a first name and a last name—you can invoke the shell program with only one parameter, the first name. However, you cannot invoke it with only the second parameter, the last name.
Here is a shell program called mypgm1
, which takes only one parameter (a name) and displays it on the screen:
#!/bin/sh#Name display programif [ $# -eq 0 ]then echo "Name not provided"else echo "Your name is "$1fi
If you execute mypgm1
, as follows
matthew@seymour:~$ bash mypgm1
you get the following output:
Name not provided
However, if you execute mypgm1
, as follows
matthew@seymour:~$ bash mypgm1 Heather
you get the this ...
Get Ubuntu Unleashed 2015 Edition: Covering 14.10 and 15.04, Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.