Supervisor
Administrator
- Apr 27, 2015
- 1,863
- 2,546
- 335
The big advantage using functions is using them multiple times, but only writing the code once
Remember that functions always have to precede "normal" code.
There are two different ways you can use functions in bash:
Remember that functions always have to precede "normal" code.
There are two different ways you can use functions in bash:
- function function_name {
command...
} - function_name (){
command...
}
Code:
#!/bin/bash
foo="1"
foobar (){
printf "variable foo is \"$foo\""
if [ -n "$1" ]
then printf " and you called this function with \$1=\"$1\".\n"
else printf " and you did not call this function with a variable. Why didn't you do that? It would have been funny :(\n"
fi
}
printf "Type \"foobar variable\": "
read -r answer
$answer
exit