Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash built in function bash source code

How to find source of a built in bash function?

I know that it is a function:

$type -t MY_APP
function

I see it's code:

type MY_APP
code

The questions are:

  1. Where is it stored?
  2. How can I modify it?
like image 239
idobr Avatar asked Feb 15 '13 14:02

idobr


People also ask

What is the use of source in Bash?

About source. source is a builtin command of the Bash shell. It takes a file name, and executes the commands in that file as if they had been typed on the command line. Using source to execute the commands in a file is not the same as running a script.

How do I add a function to a bash script?

In Bash, defining a function is as easy as setting it either in the script file you're writing or in a separate file. If you save functions to a dedicated file, you can source it into your script as you would include a library in C or C++ or import a module into Python.

How do I create my own Bash shell commands?

You can make modifications in configuration files, such as ~/.bashrc and ~/.profile, you can run services at startup, and you can create your own custom commands or script your own Bash functions. Bash (along with some other shells) has a built-in command called source.

What is a Bash function?

Think of bash functions as small snippets of code that save you time and space by eliminating the need to constantly type or copy the same sets of commands. However, the capabilities of the functions are much wider. In particular, we are talking about passing arguments to them.


1 Answers

You can do it like this:

# Turn on debug
$ shopt -s extdebug

# Print out the function's name, line number and file where it was sourced from
$ declare -F my_function
my_function 46 /home/dogbane/.bash/.bash_functions

# Turn off debug
shopt -u extdebug

To edit the function, open the file containing the function definition (that you found from above). Edit the function and save the file. Then source it into your shell, like this:

$ . /path/to/function_file
like image 102
dogbane Avatar answered Sep 22 '22 01:09

dogbane