Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note that while a function can have many arguments, the function definition never contains anything in its ( ) "formal argument list".

And in bash, arguments passed to both scripts and functions are not enclosed in parentheses, as is the case in most programming languages.

Example:

Code Block
languagebash
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; }
myfn foo bar baz

...

The 1st (sub-command name) argument is captured in the CMD variable. Calling shift then removes that 1st argument, so that  $@ now contains everything after the sub-command name – these . So script arguments 2, 3, etc., will be positional arguments 1, 2, etc., to whatever function is called.

...

As we extend our command processing script, we'll add clauses to the case/esac block and add a short usage description to the usage function.

Note that in bash, arguments to both scripts and functions are not enclosed in parentheses, as is the case in most programming languages.

Calling a function or a script

Functions and scripts are called

Calling a function or a script

Functions and scripts are called without parentheses around their arguments. Instead, each argument is separated by whitespace (one or more space characters).

...

Code Block
languagebash
# Call a custom script passing 2 arguments
my_script.sh arg1 "arg2 has embedded spaces"

# Call a function passing 2 arguments
my_function arg1 'arg2 has embedded spaces'

Example:

Code Block
languagebash
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; }
myfn foo bar baz wonk
myfn foo "bar baz" wonk
myfn "foo bar" baz wonk

Running the step_01.sh script

...