help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Difference between "argument" and "parameter"?


From: Peggy Russell
Subject: Re: [Help-bash] Difference between "argument" and "parameter"?
Date: Wed, 19 Feb 2014 08:02:09 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

> I see both "argument" and "parameter" are used. It seems to me that
> they are synonymous. Are there any subtle differences?

There are parameters and there are Bash Parameters.

The gnu reference may be helpful, or man page. 

In bash, "A parameter is an entity that stores values." 
See sections on Positional and Special Parameters.
https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameters

This is not a definition per se, but rather an example of the terms.
"When Bash runs a shell script, it sets the special parameter 0 to the name
of the file, rather than the name of the shell, and the positional
parameters are set to the remaining arguments...". 
https://www.gnu.org/software/bash/manual/bashref.html#Shell-Scripts

Scroll down the list of commands in Section 4 and see the format: 
command argument(s). Might also see term option-arguments. Checkout getopts.
https://www.gnu.org/software/bash/manual/bashref.html#index-getopts

In general, variables are referred to parameters when you define a function
and arguments when you use the function.

# Display input arguments
#
#   @param $1 param 1 definition
#   @param $2 param ...
#
function get_input() {
  # Assign positional parameters to variables
  local -- var1="${1:?Error: required argument missing}"
  local -- var2="${2:?Error: required argument missing}"
  local -- var3="${3:?Error: required argument missing}"
 
  printf -- '1: %s 2: %s 3: %s\n' "${var1}" "${var2}" "${var3}"

  # Alternatively... "$@" is assumed if no 'in WORDS'.
  local -i p=1
  for arg; do
    printf -- '%d: %s\n' $((p++)) "${arg}"
  done

  return 0
}

# Set positional parameters to arguments
# Could have supplied arguments on command line
set -- apple banana cantaloupe

# Special parameter supplies arguments to function
get_input "address@hidden"

# Could have supplied arguments directly
get_input  'apple' 'banana' 'cantaloupe'





reply via email to

[Prev in Thread] Current Thread [Next in Thread]