bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how are aliases exported?


From: Pierre Gaston
Subject: Re: how are aliases exported?
Date: Sat, 14 Apr 2012 09:33:41 +0300

On Sat, Apr 14, 2012 at 8:31 AM, Pierre Gaston <pierre.gaston@gmail.com>wrote:

>
>
> On Sat, Apr 14, 2012 at 3:44 AM, Linda Walsh <bash@tlinx.org> wrote:
>
>>
>>
>> Dennis Williamson wrote:
>>
>>  Aliases are intended for command line convenience. You should use
>>> functions, which can be exported and are the correct thing to use in
>>> scripts (and even from the command line).
>>>
>>> "For almost every purpose, shell functions are preferred over aliases."
>>>
>>> But, of course, you know that already.
>>>
>>
>> ---
>>        Yeah... and I've already demonstrated the 'almost' part.
>>
>> It's one of those:
>>
>> function _include_h { return "source <liblookup>$1" ;}
>>
>>
>> alias include='eval $( _include_h "$1")'
>>
>> Near as I can tell, you can't do that in a function.
>> If you source a file in a function, the local vars in the file
>> would be local to the function -- not to the prog using the alias
>>
>>
>> local vars in the file? what is this?
>

Oh I get it, the non working code put me off (returning a string really?)

You mean if you have "declare var=foo" in a file and then source it from a
function the variable will be local to the function, newer bash versions
have a -g option to work around this.

Anyway, as to export an alias there are 2 cases:

1) interactive bash
These source .bashrc so you can put your aliases there

2) non-interactive bash
Aliases are off by default. So given that you need to run something at the
beginning of your new bash instance anyway, you could define your aliases
in a function together with the mandatory shopt, eg:

function start_aliases {
       shopt -s expand_aliases
       alias foo=ls
}
export -f start_aliases

Then you can do:
bash  <<< $'start_aliases\nfoo'

Note that the function need to be on a line before the first use of the
alias, eg bash -c 'start_aliases;foo' doesn't work.

You can even make a kinda of export alias function with a hack like:
function start_aliases {
       shopt -s expand_aliases
       eval "$my_aliases"
}
export -f start_aliases

function exportalias {
   export my_aliases+=$'\n'"$(alias "$1")"
}
alias bar=ls
exportalias bar


reply via email to

[Prev in Thread] Current Thread [Next in Thread]