This commit is contained in:
xPMo 2020-10-25 02:59:46 -04:00 committed by GitHub
commit 4598105503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -160,6 +160,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
* [Get the list of functions in a script](#get-the-list-of-functions-in-a-script)
* [Bypass shell aliases](#bypass-shell-aliases)
* [Bypass shell functions](#bypass-shell-functions)
* [Copy shell functions](#copy-shell-functions)
* [Run a command in the background](#run-a-command-in-the-background)
* [Capture function return without command substitution](#capture-the-return-value-of-a-function-without-command-substitution)
* [AFTERWORD](#afterword)
@ -2158,6 +2159,30 @@ ls
command ls
```
## Copy shell functions
This will copy the declaration of the first argument into the second.
```sh
cp_function() {
test -n "$(declare -f "$1")" || return
eval "${_/$1/$2}"
}
```
**Example Usage:**
Keep in mind that `cp_function` will not change the body of the given function, so recursive calls will not be translated.
```shell
$ f(){ [[ $1 -le 1 ]] && echo 1 || echo "$(( $1 * $(f $(($1 - 1))) ))"; }
$ cp_function f factorial; declare -f factorial
factorial ()
{
[[ $1 -le 1 ]] && echo 1 || echo "$(( $1 * $(f $(($1 - 1))) ))"
}
```
## Run a command in the background
This will run the given command and keep it running, even after the terminal or SSH connection is terminated. All output is ignored.