Bash - Function

Bash Liste Des Attaques Ovh

About

A shell function is an object that:

By convention, the function name starts with an underscore.

Syntax

[ function ] name () compound-command [redirection]

where:

  • function is a optional reserved word
  • name is the function name.
  • () If the function reserved word is supplied, the parentheses are optional.
  • compound-command is the body of the function. Usually a list of commands between { and }, but may be any compound command. compound-command is executed whenever name is specified as the name of a simple command.
  • redirection. Any redirections specified when a function is defined are performed when the function is executed.

Feedback

Bash functions don't return anything or store value in variable, they only:

  • have an exit status
  • and produce output streams.

Exit Status

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 that can be overwritten with the return function

Capturing Value

Subshell

The function is executed in a SubShell

foo() {
   echo "Nico"
}
x=$(foo)
echo "foo returned '$x'"

Global Variable

foo() {
   return="Return Value"
}
foo
echo "foo returned '$return'"

Local Variable

Bash - local - Variable declaration in function - (Builtin)

bar() {
   var=$(($1+$2))
}

foo() {
   local var
   bar 6 2
   echo "$var"
}

foo
8

File redirection

Storing the value in a file.

# The function
foo() {
   echo "Returned Value" > "$1"
}

# The temp file
tmpfile=$(mktemp)

# The function call
foo "$tmpfile"

# Retrieving the value
echo "foo returned '$(<"$tmpfile")'"

# Close (removing the resource)
rm "$tmpfile"
foo returned 'Returned Value'

Features

  • Bash functions don't return anything or store value in variable, they only produce output streams.
  • Passable: Nnothing is “passable”, especially not arrays. Bash uses strictly call-by-value semantics (magic alias hack excepted).
  • Scope: functions are always global (have “file scope”), so no closure.
  • Nesting: Function definitions may be nested, but these are not closures, though they look very much the same.
  • Functions are not passable (first-class),
  • Namespace: Reusable functions can't be guaranteed free of namespace collisions unless you resort to weird naming rules to make conflicts sufficiently unlikely.

Documentation / Reference

help function





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - (Name|Identifier)

A word consisting only of alphanumeric characters andunderscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier. A name identify: a variable ...
Bash Liste Des Attaques Ovh
Bash - Alias (of a command) - Builtin command

Alias allows to define shortcuts and synonyms for commonly used: shell commands (of group of command) or script The basic syntax is: where: p is to only print the aliases [name[=value]...
Bash Liste Des Attaques Ovh
Bash - Caller - Stack Trace (Builtin command)

Caller is a builtin command that returns the context (localization) of any active subroutine call (a shell function or a script executed with the . or source builtins. The current frame is frame...
Bash Liste Des Attaques Ovh
Bash - Command (The Builtin command command)

command The command function suppress the the shell function lookup in the search command order. For command as a bash definition, see . where: command is the command to execute. Only builtin...
Bash Liste Des Attaques Ovh
Bash - Complete (Builtin command) - Completion

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence The process of applying these completion specifications...
Bash Liste Des Attaques Ovh
Bash - Declare (Variable declaration and attribute)

Declare variables and/or give them attributes. where: names: If no names are given then display the values of variables. p will display the attributes and values of each name. When -p is used,...
Bash Liste Des Attaques Ovh
Bash - Export (Builtin command)

The supplied names (variable or function) are marked for automatic export to the environment of subsequently executed commands. declaringx -f: the names refer to functions. -p will print a...
Bash Liste Des Attaques Ovh
Bash - Flow statement (Control Structure)

Bash - Flow statement (Control Structure) The words that control the flow of statement (ie condition and loop). help * * * * * * see * * ...



Share this page:
Follow us:
Task Runner