Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting a function in shell

Please tell me how to export a function in parent shell (bash, sh or ksh) so that the function will be available to all the child process launced from the parent process?

like image 677
Sachin Chourasiya Avatar asked Dec 11 '09 04:12

Sachin Chourasiya


People also ask

How do you export a function in Linux?

To use a function with the export command, use the -f option. If we do not use this option, it will be considered as a variable, not function. Syntax: export -f function_name.

How do you export from shell?

You can use the export command to make local variables global. To make your local shell variables global automatically, export them in your . profile file. Note: Variables can be exported down to child shells but not exported up to parent shells.

What does export mean in shell script?

Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.

What is the export command?

The export command, on the other hand, provides the ability to update the current shell session about the change you made to the exported variable. You don't have to wait until new shell session to use the value of the variable you changed. Follow this answer to receive notifications.


4 Answers

The export -f feature is specific to Bash:

parent

#!/bin/bash plus1 () { echo $(($1 + 1)); } echo $(plus1 8) export -f plus1 ./child 14 21 

child

#!/bin/bash echo $(plus1 $(($1 * $2)) ) 
like image 125
Dennis Williamson Avatar answered Sep 19 '22 00:09

Dennis Williamson


In sh, it is not possible to export a function, as noted by Charles Duffy.

like image 39
anol Avatar answered Sep 23 '22 00:09

anol


Functions are not exportable by nature. However you can export strings, so I have a little trick here:

func="$(typeset -f funcname)"
export func

To import the function, re-define it from the exported string:

# in subshell
eval "$func"
like image 24
iBug Avatar answered Sep 21 '22 00:09

iBug


If you are using ksh or zsh:

You can use the environment variable FPATH, wherein you can place all your functions.

If FPATH is set on an interactive interpreter, and a command or function is not found in the current shell environment or the PATH, the directories listed there are searched for the existence of a file named after the missing command. If one is found, it is sourced in the current shell environment, and expected to define the function.

So, you can place all your functions in a location in FPATH, and child scripts will also be able to find it.

You can use the autoload command in shell scripts to load the functions you require:

autoload fun_a fun_b

In zsh, autoload is required for FPATH to work. In ksh and its close relatives, I believe it simply causes functions defined in FPATH to override regular command in your PATH, as they would if defined directly.

Some details on FPATH and autoload:

  • http://docstore.mik.ua/orelly/unix3/upt/ch29_13.htm
  • http://users.speakeasy.net/~arkay/216-7.4KshFunctions.html
like image 40
Venkataramesh Kommoju Avatar answered Sep 20 '22 00:09

Venkataramesh Kommoju