PDA

View Full Version : [SOLVED] BASH_FUNC__ functions in environment



SteveT
26th November 2014, 04:11 PM
FC20


I'm not sure what is causing this, or even if it is normal behaviour, but from time to time whilst working in a gnome terminal session, I will get BAS_FUNC__blah functions appearing in my 'environment'. At first I wasn't aware that they were BASH_FUNC functions, as I typicall alias env to env|sort, so all I originally saw were some very odd settings in the environment.
After unaliasing env, I can see things like:

BASH_FUNC__rcs()=() { local cur prev words cword;
_init_completion || return;
local file dir i;
file=${cur##*/};
dir=${cur%/*};
[[ $file == $dir ]] && dir=.;
COMPREPLY=($( compgen -f "$dir/RCS/$file" ));
for ((i=0; i < ${#COMPREPLY[@]}; i++ ))
do
file=${COMPREPLY[$i]##*/};
dir=${COMPREPLY[$i]%RCS/*};
COMPREPLY[$i]=$dir$file;
done;
COMPREPLY+=($( compgen -G "$dir/$file*,v" ));
for ((i=0; i < ${#COMPREPLY[@]}; i++ ))
do
COMPREPLY[$i]=${COMPREPLY[$i]%,v};
done;
[[ ${#COMPREPLY[@]} -eq 0 && $1 == ci ]] && _filedir || _filedir -d
}

When I do an 'env'.

Any ideas as to how/why these functions are being set as they only appear to appear intermittently? (This morning I had one setup for BASH_FUNC__sudo).

sea
26th November 2014, 04:28 PM
Please use CODE TAGS when you post code, helps alot to read.

Looks very much like bash completition, eventhough the start of the function looks very weird...
Bash function examples:

func_one() { echo "func one"; }

somethingelse() {
echo "something else"
}

# ---------------
# And a nice one, giving a syntax example and description:
# ---------------

my_func() { # ID "MSG"
# Expects an ID as integer and a MSG as string
# it prints them out
[[ $# -lt 2 ]] && echo "Requires: ID 'MSG'" && return 1
ID=$1
MSG="$2"
echo "My func: $ID $MSG"
return 0
}

I've just accomplished my first bash completition myself and shared that experience,
have a look at: http://forums.fedoraforum.org/showthread.php?t=301595

Hope this helps

sea
26th November 2014, 05:52 PM
Ah yes, about the not beeing visible in 'env', try this:

# if its in a file:
source ./file
export -f function_name

# if in the shell or inside the script, note the script requires to be sourced, not executed, in the shell to provide the function to the 'env.
func_test() { echo yay;}
export -f func_test

Hope this helps

SteveT
26th November 2014, 06:02 PM
I am not sure I follow the replies.

The functions that appear in the 'env' listing are not functions that I've written - they appear to be related to things like sudo and rcs and appear 'randomly'.
The problem isn't that the functions DO NOT appear - it's the opposite, that spurious functions appear in the env that I am not aware of adding there IE for some reason things like RCS and SUDo are creating entries in my environment.

Does that make more sense?

sea
26th November 2014, 10:28 PM
Ahh hhahaa ah yeah, figured now what you mean. :D

For example:
1) I have a custom PS1, actualy a script provding diffrent functions.

env|grep BASH
BASH_FUNC_return_status_color()=() { case "$1" in
BASH_FUNC_func_test()=() { echo yay
BASH_FUNC_return_user_color()=() { [ 0 -eq $UID ] && echo "${whitebg}${redfont}" || echo "${whitebg}${bluefont}"
BASH_FUNC_rnd()=() { [ -z $1 ] && echo "Usage: rnd MAXNUMBER [MINVALUE]" && return 1 || max=$1;
BASH_FUNC_return_status_string()=() { good=('+' ':)' '✔');


2) When you are testing bash completition, as in writing one, it may produce output as you shown.
Though, you would need to source the file, which also exports the function it contains, to actualy get it to the env of the running system.

Hope this helps

SteveT
27th November 2014, 08:40 AM
Sea,
But I'm not writing any functions. I am running scripts that may or may not contain functions. The functions that appear in the 'env' are not any that I have written - so the question is '...when/how/why does bash add those functions to the environment..' and is it 'normal' for them to appear (and what is the implication of them being there)?

My typical day uses RCS and ci/co, sudo, ls, cd - but I don't then always get a BASH_FUNC__sudo appear in the env after every sudo command I perform, I don't get a BASH_FUNC__rcs after every check in or check out - BUT, I do get them intermittently.

sea
27th November 2014, 04:52 PM
The naming of them this way is new, but they 'get' there upon calling of scripts.
Specialy when those scripts export functions.

So yes, it is (generaly) a normal behaviour.
Its the way it works.

SteveT
28th November 2014, 02:44 PM
Ok, this appears to be caused by 'me'. The scripts that are appearing in my environment are the bash completion scripts for rcs and sudo - they are found here: /usr/share/bash-completion/completions

Presumably because of the shellshock vulnerability, Fedora implemented the addition of BASH_FUNC_ to the front of the script name .

They would not normally appear in the 'exported' environment, but in my case, the .bash_profile script has used 'set -a' (visible via echo "$-") to export settings from an external set of files - and that had been left set in my terminal session. So when bash completion, for whatever reason, deemed it necessary to load any completion script, they were being exported to 'my environment'.

The obvious fix was to stop the use of set -a in the profile.

sea
28th November 2014, 04:59 PM
:doh: I didnt write that with 'bash_func', eventhough i was thinking about it.

Just as i said in the beginning, but mis-understood/interpreted the post, that code is just bash completion :)

Though, exported functions still do appear in 'env', but recieve just as well a 'bash_func' in front of them.
I do have my PS1 customized using exported functions and variables, and those functions start (in the 'env' only) as well with 'bash_func'.

Thanks for sharing the solution.