YouTip LogoYouTip

Linux Shell Func

# Shell Functions Linux shell allows users to define functions, which can then be called freely within shell scripts. The format for defining a function in shell is as follows: funname [()] { action; [return int;] } Explanation: * 1. You can define it with `function fun()` or simply `fun()` without any parameters. * 2. For parameter return, you can explicitly use **return** to return a value. If not used, the result of the last command executed will be the return value. **return** is followed by a numeric value `n` (0-255). The following example defines a function and calls it: ## Example ```bash #!/bin/bash # author:Tutorial # url:example.com demoFun(){ echo "This is my first shell function!" } echo "-----Function execution starts-----" demoFun echo "-----Function execution completed-----" Output: -----Function execution starts----- This is my first shell function! -----Function execution completed----- The following example defines a function with a return statement: ## Example ```bash #!/bin/bash # author:Tutorial # url:example.com funWithReturn(){ echo "This function will add the two input numbers..." echo "Enter the first number: " read aNum echo "Enter the second number: " read anotherNum echo "The two numbers are $aNum and $anotherNum!" return $(($aNum+$anotherNum)) } funWithReturn echo "The sum of the two input numbers is $?!" Output similar to: This function will add the two input numbers... Enter the first number: 1 Enter the second number: 2 The two numbers are 1 and 2! The sum of the two input numbers is 3! The function's return value is obtained via `$?` after calling the function. **Note:** All functions must be defined before they are used. This means you must place the function at the beginning of the script, and it can only be used once the shell interpreter has encountered it. To call a function, you only need to use its function name. **Note:** The `return` statement can only return an integer between 0 and 255. The sum of two input numbers might exceed this range. To solve this problem, you can modify the `return` statement to directly output the sum using `echo` instead of using `return`: ## Example ```bash funWithReturn(){ echo "This function will add the two input numbers..." echo "Enter the first number: " read aNum echo "Enter the second number: read anotherNum sum=$(($aNum + $anotherNum)) echo "The two numbers are $aNum and $anotherNum!" echo $sum # Output the sum of the two numbers } * * * ## Function Parameters In Shell, you can pass parameters to a function when calling it. Inside the function body, you can access parameter values using the `$n` format. For example, `$1` represents the first parameter, `$2` represents the second parameter, and so on. Example of a function with parameters: ## Example ```bash #!/bin/bash # author:Tutorial # url:example.com funWithParam(){ echo "The first parameter is $1!" echo "The second parameter is $2!" echo "The tenth parameter is $10!" echo "The tenth parameter is ${10}!" echo "The eleventh parameter is ${11}!" echo "The total number of parameters is $#!" echo "All parameters as a string: $*!" } funWithParam 1 2 3 4 5 6 7 8 9 34 73 Output: The first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! The eleventh parameter is 73! The total number of parameters is 11! All parameters as a string: 1 2 3 4 5 6 7 8 9 34 73! Note that `$10` cannot get the tenth parameter. To get the tenth parameter, you need `${10}`. When `n >= 10`, you must use `${n}` to access the parameter. Additionally, there are several special characters used to handle parameters: | Parameter Handling | Description | | --- | --- | | $# | The number of parameters passed to the script or function | | $* | Displays all parameters passed to the script as a single string | | $$ | The process ID of the currently running script | | $! | The process ID of the last background process | | $@ | Same as $*, but when used with quotes, each parameter is returned within quotes. | | $- | Displays the current options used by the shell, same functionality as the `set` command. | | $? | Displays the exit status of the last command. 0 indicates no error, any other value indicates an error. |
← Docker Compose Restart CommandDocker Compose Start Command β†’