Source functions from variable

I have a functions file containing functions:

file.functions:

myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}

i know i can source this file in another script using something similar to this:

source /tmp/file.functions
or
. /tmp/file.functions

however, suppose I have the content of /tmp/file.functions in a variable, how can I source it?

i.e.

MyFunctions=$(cat /tmp/file.functions)

Instead of MyFunctions=$(cat /tmp/file.functions) , why not just use MyFunctions=/tmp/file.functions and then use:

source "$MyFunctions"

or

. "$MyFunctions"
1 Like

ok, i provided the cat example to depict functions being in a variable.

in the case that am working on, the functions will be in a variable. i only catted them in the example to show precisely what i was aiming for.

i need to source a variable.

ok., let me put a different way.

MyFunctions='
myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}
'

can i source this variable now?

Hi,
this is just another way.

You only nee to add a semicolon at the end of each command.

myTest () { echo "MYPWD=$(echo $(pwd))"; echo "MYLOGNAME=${LOGNAME}"; } myCar () { echo "MYDATE=$(date)"; echo "MYUPTIME=$(uptime)"; }

Then:

MyFunctions=$(cat /tmp/file.functions)

eval $MyFunctions

typeset -f myTest

myTest () { echo "MYPWD=$(echo $(pwd))" echo "MYLOGNAME=${LOGNAME}" }

Greetings!

1 Like

Can I ask why you need to put functions into an "intermediate" container object? Doing what Don Cragun suggested is simpler and standard.

Your efforts will make code maintenance much harder. This may not apply to you, but don't confuse cool with good programming practice. The two are not always congruent.

@jim mcnamara

I added that way because it was the original request.

Regards.

1 Like

i really need to be able to source functions however they are stored in a variable.

i understand it shouldnt be done this way. but i have my own need to do it this way.

doesnt look like its possible :frowning:

We all know that different shells behave differently. (And we know that you haven't told us what shell or shells you're using for this problem.)

We all know that we can do some things in shell code a few different ways (or a few thousand different ways) depending on what we are trying to do. (And we know that you haven't told us what you are trying to do other than define functions that have been read from a file into a variable.)

The only thing I see here that is impossible is for us to accurately guess at why you think you have to use this method to define functions and what you are going to do with those functions after you define them.

If you would share with us what you are trying to do instead of telling us how you want to implement a small piece of what you want to do, we might be able to help you find a solution to your problem.

P.S. Did you know that some shells can export functions in addition to exporting variables?

1 Like

Hi.

Possibly:

#!/usr/bin/env bash

# @(#) s1       Demonstrate source function from variable.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

pl " Remove variable v1 and function x1:"
unset v1
unset -f x1
set | grep v1
set | grep x1

pl " Create variable:"
v1="
x1() { printf ' Hello, world from x1.\n' ; }
"

pl " Variable v1 = $v1"

pl " Results, try to execute x1, expect failure:"
x1

pl " Source, then execute x1"
source <( printf "%s" "$v1")
x1

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30

-----
 Remove variable v1 and function x1:
_i=' Remove variable v1 and function x1:'
_i=' Remove variable v1 and function x1:'

-----
 Create variable:

-----
 Variable v1 = 
x1() { printf ' Hello, world from x1.\n' ; }


-----
 Results, try to execute x1, expect failure:
./s1: line 29: x1: command not found

-----
 Source, then execute x1
 Hello, world from x1.

( With an assist from bash - source /dev/stdin doesn't work as expected - Unix & Linux Stack Exchange )

Best wishes ... cheers, drl