Find the answer to your Linux question:
Results 1 to 2 of 2
Hey guys... I'm a bit stuck, and hoping someone can help me out. I wrote some bash code to prevent redundantly including files, like php's "include_once". I've simplified it here ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6

    Question unalias not working?

    Hey guys... I'm a bit stuck, and hoping someone can help me out.

    I wrote some bash code to prevent redundantly including files, like php's "include_once". I've simplified it here so don't worry that it appears not to do anything:

    Code:
    alias .='Include'
    alias source='Include'
    export __INC__functions=1
    
    function Include {
        local name="__INC__"`basename ${1%\.*}`
        eval "export $name=1"
    
        unalias source
        source $1
        alias source='Include'
    }
    For some reason this is not working. Although I "unalias source" inside the "Include" function, the very next line is still calling "Include". Here is what I see with set -v -x:

    Code:
    ...
    ++ eval 'export __INC__mysql_functions=1'
    export __INC__mysql_functions=1
    +++ export __INC__mysql_functions=1
    +++ __INC__mysql_functions=1
    ++ unalias source
    ++ Include /home/fonix/client_progs/cs/utils/mysql_functions.sh
    ...
    the unalias appears to work, but the next call to source is treated as the original alias. Does anyone know why it would do this? Are aliases replaced within a function at the start of the function, or perhaps when the function is defined? Is there a way to get this to work?

    Thanks in advance!

  2. #2
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6
    I do see this in the MAN page which does explain the behavior:

    "Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command."

    So I guess I'll just ask the last question I had. Is there a way around this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...