Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using Ubuntu 9.10 and BASH_VERSION='4.0.33(1)-release'...

I have a bash function that I want to be able to call from my shell. I asked a different question about it here:

Ubuntu Login Screen Reloads

So now that I've fixed the syntax issues, the new problem is that the function is not being exported properly to my shell environment. So I tried moving the file from /etc/profile.d to my home dir and now I am just trying to source it from ~/.profile, like this:

. $HOME/.p4c

This doesn't have any effect - the function is still not exported. How I am checking this:

  • When I open a terminal and run set|grep p4c I don't see this function defined.
  • If I then type . $HOME/.p4c on the command line, and then run set|grep p4c again, now I can see the function exported properly to my environment.

I know that my ~/.profile is being read, because immediately above . $HOME/.p4c I am exporting an env variable and it is defined in my environment when I first open a terminal. Ugh...

Here is the contents of $HOME/.p4c:

# p4c() function setup params
p4_HOST=`hostname | awk -F . '{print $1}'`

# function for setting the P4CLIENT variable based on the first non-option
# argument provided
p4c() {
    while getopts ":hdsv" option
        case $option in
            h) echo "p4c provides information about perforce clients."
               echo "Recognized arguments:"
               echo "    -h     help (this message)"
               echo "    -d     descriptions (prints client descriptions - usefu                                                            l, but slightly slower)"
               echo "    -v     verbose (print unreasonable amounts of debugging                                                             info"
               # About to exit - reset OPTIND or we'll be in trouble later.
               # Abort
            v) VERBOSE_MODE='verbose';;
            d) DESC_MODE='descriptions';;
            s) SHORT_MODE='short';;
            *) echo "Unknown option '$OPTARG'!  Specify -h for help..."
               # About to exit - reset OPTIND or we'll be in trouble later.
               # Abort

    # Set argument pointer to first non-option argument
    shift $(($OPTIND - 1))

    # Done with OPTIND - better reset it before something bad happens...

    if [ $VERBOSE_MODE ]
        echo "PROJECT: $PROJECT"

    # Need to check/set p4_USER every time to allow changes between invocations
    if [ -z "$p4c_USER" ]
        p4_USER=`echo $P4USER`
        if [ -z "$p4_USER" ]
            p4_USER=`id -nu`
    if [ $VERBOSE_MODE ]
        echo "p4_USER: $p4_USER"

    if [ -n "$PROJECT" ]
        # provided a non empty string project name
        if [ $VERBOSE_MODE ]
            echo "p4_CLIENT: $p4_CLIENT"

        # check client to see if it exists
        p4_GREP_RESULT=`p4 clients | grep "$p4_CLIENT"`
        if [ -z "$p4_GREP_RESULT" ]
            echo "NOTE: P4 client \"$p4_CLIENT\" does not exist on server."
            echo "Setting P4CLIENT anyway so that client 
share|improve this question
Debugging: try putting the line declare -f p4c just after it is declared, to check that things are in order before the export. I'd prefer the syntax declare -fx ... to traditional shell syntax for this sort of thing, fwiw. – Charles Stewart Feb 8 '10 at 18:26
@Charles - thanks for the tip. I tried this when implementing Charles' solution below, and I did see the definition of p4c printed out. Nice to know about declare - thanks! – AJ. Feb 8 '10 at 19:18
up vote 1 down vote accepted

Have you tried sourcing it in ~/.bashrc instead of ~/.profile?

From the Bash man page:

       When  an  interactive  shell that is not a login shell is started, bash
       reads and executes commands from  /etc/bash.bashrc  and  ~/.bashrc,  if
       these  files  exist.  This may be inhibited by using the --norc option.
       The --rcfile file option will force bash to read and  execute  commands
       from file instead of /etc/bash.bashrc and ~/.bashrc.

Which would be the case when you run a terminal in an X-window session, for example.

share|improve this answer
@Dennis - thanks that totally fixed it! – AJ. Feb 8 '10 at 19:17

You must log in to answer this question.

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