You are not logged in.

#1 2009-09-02 12:03:02

Llama
Banned
From: St.-Petersburg, Russia
Registered: 2008-03-03
Posts: 1,379

bash: declare a function (globally) [SOLVED]

Hi,

I've been trying to add a function to environment:

MyDir.sh:

#!/bin/bash
my_dir() { ls -F "$@"; }
declare -fx my_dir

It doesn't appear to work, whereas the same commands done from the bash prompt deliver:

$ ./MyDir.sh
$ declare -F
$ echo $?
0
$ my_dir() { ls -F "$@"; }
$ declare -fx my_dir
$ declare -F
declare -fx my_dir
$ env | less

The env command aslo shows my_dir. Any ideas?

Last edited by Llama (2009-09-02 14:09:21)

Offline

#2 2009-09-02 12:05:23

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: bash: declare a function (globally) [SOLVED]

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

edit:  whoops.  Misunderstood.

Last edited by keenerd (2009-09-02 13:42:04)

Offline

#3 2009-09-02 12:11:42

Llama
Banned
From: St.-Petersburg, Russia
Registered: 2008-03-03
Posts: 1,379

Re: bash: declare a function (globally) [SOLVED]

Is it a link you really have meant?

Offline

#4 2009-09-02 12:25:20

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: bash: declare a function (globally) [SOLVED]

The problem is that you are calling your script. Try using

$ . ./MyDir.sh

instead of

$ ./MyDir.sh

Note the space between the two spaces in the first example. Or you can use include for greater readability.

The point is this: When you call the script (as in your initial try) a new bash process is created which executes your script. It thus reads and has the definition of your function, e.g. if you call the function from within MyDir.sh it will work. Afterwards it ends and you return to you initial bash process, which knows nothing about your function. If you include the script, the commands within it are executed in your current bash process and thus you will have your function definition.

Offline

#5 2009-09-02 12:25:21

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: bash: declare a function (globally) [SOLVED]

If you want the function in the current shell, use source, otherwise the function is only available in the shell that runs the script.

Offline

#6 2009-09-02 12:40:21

Llama
Banned
From: St.-Petersburg, Russia
Registered: 2008-03-03
Posts: 1,379

Re: bash: declare a function (globally) [SOLVED]

You are probably right, davvil, but I still don't quite get it:

$ declare -F
$ ./MyDir.sh
$ declare -F
$ declare -F
$ . ./MyDir.sh
$ declare -F
declare -fx my_dir
$ bash
$ declare -F
declare -fx my_dir
$

Another bash instance still sees the function; as soon as I kill both bash instances and start yet another one, my_dir is lost. I thought that my environment (-x) declarations are going to live as global ones till the end of my user session. I'd like to smile ...

Last edited by Llama (2009-09-02 12:41:23)

Offline

#7 2009-09-02 13:17:48

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: bash: declare a function (globally) [SOLVED]

Your environment declarations are passed to the children of your bash session. I will try to draw some ascii art of your calls

bash
|
|-- new bash (./MyDir.sh)
.     |  my_dir is known
.     |
.      \ process ends
|
| (you return to your original bash process, where my_dir is unknown)
|
| . ./MyDir.sh is run in your original bash process
| my_dir is known
|
|-- new bash (bash)
.      | my_dir is inherited from your previous bash
.      |
.     ...

If you want to have my_dir in all your instances of bash, you have to include MyDir.sh in your .bashrc. In this way it will automatically be loaded whenever you open a shell.

Offline

#8 2009-09-02 13:24:28

Llama
Banned
From: St.-Petersburg, Russia
Registered: 2008-03-03
Posts: 1,379

Re: bash: declare a function (globally) [SOLVED]

Thanks, davvil! Just one last question: is there any difference between declare -fx <name> and export -f <name>?

Offline

#9 2009-09-02 13:53:19

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: bash: declare a function (globally) [SOLVED]

Llama wrote:

Thanks, davvil! Just one last question: is there any difference between declare -fx <name> and export -f <name>?

Not that I know of (just checked the man page).

Offline

#10 2009-09-02 14:23:27

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: bash: declare a function (globally) [SOLVED]

From "help declare":
    When used in a function, `declare' makes NAMEs local, as with the `local'
    command.

However, I just tested this and function definitions seem to bleed into the global scope, despite the use of declare -f. This happens regardless of whether I put the declare after the function definition as Llama does, or before it, as I always thought was correct.

$ alpha() { echo alpha; }

$ gamma() { alpha; }

$ beta() {  declare -f alpha >/dev/null; alpha() { echo beta; }; gamma; }

$ alpha
alpha

$ beta
beta
# as expected, but...

$ alpha
beta
# the redefinition of alpha has bled outside of the scope of beta()

Offline

Board footer

Powered by FluxBB