Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add copying shell functions #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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)
* [AFTERWORD](#afterword)

Expand Down Expand Up @@ -1981,6 +1982,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.
Expand Down