Functions
- Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command.
- When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed.
- Shell functions are executed in the current shell context; no new process is created to interpret them. This is similar to running using . Or source builtin.
- Functions are declared using this syntax:
name () compound-command [ redirections ]
or
function name [()] compound-command [ redirections ]
This
defines a shell function named name. The reserved word function is optional. Without
the keyword function, the () indicate that this is a function.
If the
function reserved word is supplied, the parentheses are optional.
- The body of the function is
the compound command compound-command.
That command is usually a list enclosed between { and }, but may be any compound command like if,case etc. compound-command is executed whenever name is specified as the name of a command.
- Any redirections associated with the shell function are performed when the function is executed.
- A function definition may be
deleted using the -f option to the unset builtin
- With declare, functions can
be made readonly also.
[sukul1@blph512 teradata]$ readonly showme
[sukul1@blph512 teradata]$ unset showme
[sukul1@blph512 teradata]$ unset showme
-bash: unset: showme: cannot unset: readonly variable
[sukul1@blph512 teradata]$ declare +r showme
-bash: declare: showme: readonly variable
[sm017r@blph512 teradata]$
Readonly functions cannot be unset or made non-readonly
- The exit status of a function
definition is zero unless a syntax error occurs or a readonly function
with the same name already exists.
- When executed, the exit status of a function is the exit status of the last command executed in the body.
- In the most common usage the curly braces that surround the body of the function must be separated from the body by blanks or newlines. This is because the braces are reserved words and are only recognized as such when they are separated from the command list by whitespace or another shell metacharacter.
- When a function is executed,
the arguments to the function become the positional parameters during its
execution.
The special parameter ‘#’ that expands to the number of positional parameters is updated to reflect the change.
Special parameter 0 is unchanged.(very imp)
[sukul1@blph512 teradata]$ function showme ()
> {
> echo $0 $LOGNAME
> }
[sukul1@blph512 teradata]$ showme
-bash sukul1
Note that value of $0 is not the function name, its the shell
name.
If this is run inside a script, $0 will be script name.
If this is run inside a script, $0 will be script name.
- The first element of the FUNCNAME variable is set to the name of the function while the function is executing.
- The FUNCNEST variable, if set to a numeric value greater than 0, defines a maximum function nesting level. Function invocations that exceed the limit cause the entire command to abort.
- If the builtin command return is executed in a function, the function completes and execution resumes with the next command after the function call.
- When a function completes, the values of the positional parameters and the special parameter ‘#’ are restored to the values they had prior to the function’s execution.
- If a numeric argument is given to return, that is the function’s return status; otherwise the function’s return status is the exit status of the last command executed before the return.
- Variables local to the function may be declared with the local builtin. These variables are visible only to the function and the commands it invokes
- Function names and
definitions may be listed with the -f option to the declare (typeset)
builtin command
[sukul1@blph512 teradata]$ declare -f showme
showme ()
{
echo $0 $LOGNAME
}
- The -F option to declare or typeset will list the function names only
[sukul1@blph512 teradata]$ declare -F
declare -f _module
declare -f _module_avail
declare -f _module_long_arg_list
declare -f _module_not_yet_loaded
declare -fx module
declare -f
showme
- Functions may be exported so that subshells automatically have them defined with the -f option to the export builtin
[sukul1@blph512 teradata]$ export -f showme
[sukul1@blph512 teradata]$ declare -F
declare -f _module
declare -f _module_avail
declare -f _module_long_arg_list
declare -f _module_not_yet_loaded
declare -fx module
declare -fx
showme
So
functions can be declared, exported and unset just like variables.
- With declare, functions can
be made readonly also.
[sukul1@blph512 teradata]$ readonly showme
[sukul1@blph512 teradata]$ unset showme
[sukul1@blph512 teradata]$ unset showme
-bash: unset: showme: cannot unset: readonly variable
[sukul1@blph512 teradata]$ declare +r showme
-bash: declare: showme: readonly variable
[sukul1@blph512 teradata]$
Readonly functions cannot be unset or made non-readonly
- Functions may be recursive. The FUNCNEST variable may be used to limit the depth of the function call stack and restrict the number of function invocations. By default, no limit is placed on the number of recursive calls.
Nice post ! Thanks for sharing valuable information with us. Keep sharing..Big Data Hadoop Online Training
ReplyDelete