Differences between revisions 1 and 3 (spanning 2 versions)
Revision 1 as of 2008-05-14 13:52:19
Size: 6486
Editor: Lhunath
Comment:
Revision 3 as of 2008-11-11 21:40:39
Size: 419
Editor: cpe-69-206-242-237
Comment: 7
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:

[[Anchor(Parameters)]]
== Parameters ==

Parameters should be seen as a sort of named space in memory where you can store your data. Generally speaking, they will store string data, but can also be used to store integers or arrays.

--------
 . ''Parameters'': Parameters store data that can be retrieved through a symbol or a name.
--------

=== Special Parameters and Variables ===
Let's get our vocabulary straight before we get into the real deal. There are ''Parameters'' and ''Variables''. Variables are actually just a kind of parameters: parameters that are denoted by a name. Parameters that aren't variables are called ''Special Parameters''. I'm sure you'll understand things better with a few examples:

{{{
    $ # Some parameters that aren't variables:
    $ echo My shell is $0, and was started with these options: $-
    My shell is -bash, and was started with these options: himB
    $ # Some parameters that ARE variables:
    $ echo I am $LOGNAME, and I live at $HOME.
    I am lhunath, and I live at /home/lhunath.
}}}
'''Please note: Unlike PHP/Perl/... parameters do NOT start with a $-sign. The $-sign you see in the examples merely causes the parameter that follows it to be ''expanded''. Expansion basically means that the shell replaces it by its content. As such, `LOGNAME` is the parameter (variable), that contains your username. `$LOGNAME` will be replaced with its content; which in my case, is `lhunath`.'''

I think you've got the drift now. Here's a summary of most ''Special Parameters'':

 * '''0''': Contains the name of the script. (This is not always reliable.)
 * '''Positional Parameters''': 1, 2, ...; They contain the arguments that were passed to the current script.
 * '''*''': Expands to all the words of all the positional parameters. If double quoted, it expands to a single string containing all the positional parameters separated by the first character of the '''IFS''' variable (which will be discussed later).
 * '''@''': Expands to all the words of all the positional parameters. If double quoted, it expands to a list of all the positional parameters as individual words.
 * '''#''': Expands to the number of positional parameters that are currently set.
 * '''?''': Expands to the exit code of the most recently completed foreground command.
 * '''$''': Expands to the ["PID"] (process ID number) of the current shell.
 * '''!''': Expands to the ["PID"] of the application most recently executed in the background.
 * '''_''': Expands to the last argument of the last command that was executed.
And here are some examples of ''Variables'' that the shell initializes for you:

 * '''BASH_VERSION''': Contains a string describing the version of ["BASH"].
 * '''HOSTNAME''': Contains the hostname of your computer, I swear.
 * '''PPID''': Contains the ["PID"] of the parent process of this shell.
 * '''PWD''': Contains the current directory.
 * '''RANDOM''': Each time you expand this variable, a random number between 0 and 32767 is generated.
 * '''UID''': The ID number of the current user.
 * '''COLUMNS''': The number of characters that fit on one line in your terminal. (The width of your terminal in characters.)
 * '''LINES''': The number of lines that fit in your terminal. (The height of your terminal in lines.)
 * '''HOME''': The current user's home directory.
 * '''PATH''': A colon-separated list of paths that will be searched to find the executable for a command that is executed, if it is not an alias, function or builtin command (or absolutely referenced).
 * '''PS1''': Contains a string that describes the format of your shell prompt.
 * '''TMPDIR''': Contains the directory that is used to store temporary files (by the shell).
Of course, you aren't restricted to only these variables. Feel free to define your own:

{{{
    $ country=Canada
    $ echo "I am $LOGNAME and I currently live in $country."
    I am lhunath and I currently live in Canada.
}}}
Notice what we did to assign the value `Canada` to the variable `country`. Remember that you are '''NOT allowed to have any spaces before or after that equals sign'''!

{{{
    $ language = PHP
    -bash: language: command not found
    $ language=PHP
    $ echo "I'm far too used to $language."
    I'm far too used to PHP.
}}}
Remember that ["BASH"] is not Perl or PHP. You need to be very well aware of how ''expansion'' works to avoid '''big''' trouble. If you don't, you'll end up creating very dangerous situations in your scripts, especially when making this mistake with `rm`:

{{{
    $ ls
    no secret secret
    $ file='no secret'
    $ rm $file
    rm: cannot remove `no': No such file or directory
}}}
Imagine we have two files, `no secret` and `secret`. The first contains nothing useful, but the second contains the secret that will save the world from impending doom. Unthoughtful as you are, you forgot to '''quote''' your parameter expansion of `file`. ["BASH"] expands the parameter and the result is `rm no secret`. ["BASH"] splits the arguments up by their whitespace as it normally does, and `rm` is passed two arguments; 'no' and 'secret'. As a result, it fails to find the file `no` and it deletes the file `secret`. ''The secret is lost!''

--------
 . '''Good Practice: [[BR]] You should always keep parameter expansions well quoted. This prevents the whitespace or the possible globs inside of them from giving you gray hair or unexpectedly wiping stuff off your computer. The only good PE, is a quoted PE.'''
Hello. I hope to meet you at the Autosport Show, I plan to attend the Thursday and Sunday colloidal silver good or bad So, good luck to your team! http://blow49through-ym.tripod.com/gold2a3.html lord of the rings ps2 walkthrough many, http://infostuff.webng.com/gold7e7.html press release on jewelry for dogs class, http://infostuff.50webs.com/goldfa6.html toledo gold of spain, Best regards,
Line 79: Line 3:
 . '''In The Manual: [http://www.gnu.org/software/bash/manual/bashref.html#SEC23 Shell Parameters], [http://www.gnu.org/software/bash/manual/bashref.html#SEC60 Shell Variables]'''
----
 . '''In the FAQ: [[BR]] [http://wooledge.org/mywiki/BashFAQ/013 How can I concatenate two variables? How do I append a string to a variable?] [[BR]] [http://wooledge.org/mywiki/BashFAQ/025 How can I access positional parameters after $9?]'''
----
 . ''Variable'': A variable is a kind of parameter that you can create and modify directly. It is denoted by a name, which must begin with a letter, and must consist only of letters, digits, and the underscore (`_`). Variable names are case-sensitive. [[BR]] ''Expansion'': Expansion happens when a parameter is prefixed by a dollar sign. ["BASH"] takes the parameter's value and replaces the parameter's expansion by its value before executing the command.
--------
CategoryHomepage

Hello. I hope to meet you at the Autosport Show, I plan to attend the Thursday and Sunday colloidal silver good or bad So, good luck to your team! http://blow49through-ym.tripod.com/gold2a3.html lord of the rings ps2 walkthrough many, http://infostuff.webng.com/gold7e7.html press release on jewelry for dogs class, http://infostuff.50webs.com/goldfa6.html toledo gold of spain, Best regards,


CategoryHomepage

BashGuide/Parameters (last edited 2017-04-07 00:32:44 by pool-71-188-87-188)