Register
It is currently Tue Oct 21, 2014 9:13 pm

Positional Parameters (arguments)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Mar 27, 2008 11:36 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Let's discuss positional parameters built into the bash shell. Parameters are often called arguments. But since we are dealing with the bash shell, I will continue to refer to them as parameters. Positional parameters are referred to by using $ followed by a number. Positional parameters are space delimited. From your command line, say you were to use a command like ls. You want to list the directories, but you want to also list the permissions on all the files and directories as well. You would have to use the argument -l (that's a lower case L). So on the command line you now have: ls -l Those are positional parameters 0 and 1. I know, most people count by starting with 1, but a computer is not a person. Arrays, indexes, lists, and positional parameters are started with 0 as the first position. Let's do a little script to show positional parameters at work. You can just copy and paste what I have in the code block below.
Code:
#!/bin/bash

# first we want the total number of
# parameters passed to our script
loopCount=$#;

# but we want to ignore the 0 parameter
# so our count is one less than reported
((loopCount--))

# we want to echo back the name of the script
# shameless advertisements :)
echo ":: You are currently running: $0"

# now we want to loop through all the parameters
# listed after the call to our script
for ((i=0; i <= $loopCount; i++))
do
   # we will echo the parameter in position 1
        echo $1;
   
   # next we will shift all the parameters down
   # so that what was position 2 is now
   # position 1.  that way we can just echo $1
   # everytime.
        shift 1;
done

Now that we have that script written, let's name it listParams.sh.
Let's also run it with the arguments bob and bill.
Code:
<- jb:/home/jbsnake/scripts -> sh listParams.sh bob bill
:: You are currently running: listParams.sh
bob
bill
<- jb:/home/jbsnake/scripts ->

You can run it with as many things listed after the script name as you want. Just remember that it is space delimited, meaning, what seperates one parameter from another is a space. So if you want more than one thing listed on one line, you need to incase the entire thing in quotation marks "". Ex:
Code:
<- jb:/home/jbsnake/scripts -> sh listParams.sh "parameter 1 is here" "parameter 2 is here" parameter 3 is messed up
:: You are currently running: listParams.sh
parameter 1 is here
parameter 2 is here
parameter
3
is
messed
up
<- jb:/home/jbsnake/scripts ->

Pretty easy if you think about it. What's that you say? Can you shift more than one place? Ofcourse you can. :) Any number listed after the shift command will cause a shift of that amount. Well, unless there aren't enough places to shift. Here are the rules for shift:
man bash wrote:
shift [n]
The positional parameters from n+1 ... are renamed to $1 ....
Parameters represented by the numbers $# down to $#-n+1 are
unset. n must be a non-negative number less than or equal to
$#. If n is 0, no parameters are changed. If n is not given,
it is assumed to be 1. If n is greater than $#, the positional
parameters are not changed. The return status is greater than
zero if n is greater than $# or less than zero; otherwise 0.

Happy coding with the ability to use arguments at the command prompt now :)


Top
 Profile  
 PostPosted: Fri Jul 30, 2010 3:32 pm   

Joined: Fri Jul 30, 2010 3:19 pm
Posts: 6
$0 is a special parameter not a positional parameter (it is not an argument to the command).

The Bourne shell accepted a maximum of 9 positional parameters. This meant that $10 was interpreted as $1 followed by a literal 0.

The POSIX standard allows any number of positional parameters, but since it wanted to remain compatible with Bourne scripts, parameters greater than 9 must be enclosed in braces: ${10}, ${11), etc..


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP