3

My .bashrc file exports a function called lookup:

lookup() {
  grep -r -ne "$1" * | grep -v "TAGS:" | grep -v "tags:"
}
export -f lookup

I can use that function in a shell. But if I put it in a script like this:

#!/bin/bash
lookup "foo"

and run the script then bash reports "command not found" for lookup. This used to work some time ago. I strongly suspect this is stopped working after a security update of bash on my system (possibly related to shellshock/bashdoor?) My bash version is 4.1.10(1) on openSUSE 11.4

1 Answer 1

8

Check http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

Invoked non-interactively

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.

Specifically, none of the ~/.bashrc, ~/.profile, ~/.bash_profile are NOT sourced. ~/.bashrc is only invoked if the shell is an interactive shell.

You have a couple of options:

  1. source your .bashrc explicitly

    #!/bin/bash
    . ~/.bashrc
    lookup "foo"
    
  2. start bash with the interactive flag

    #!/bin/bash -i
    lookup "foo"
    
  3. set the BASH_ENV variable when you start your script:

    BASH_ENV=$HOME/.bashrc /path/to/my/script
    
1
  • For some reason advice no. 2 does not work if I invoke the script via ./foo.sh. It only works if I invoke via bash foo.sh. But advice no. 1 works flawlessly. Nov 16, 2015 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .