help-bash
[Top][All Lists]
Advanced

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

Re: Alias inside function gives command not found


From: Alex fxmbsw7 Ratchev
Subject: Re: Alias inside function gives command not found
Date: Thu, 14 Oct 2021 06:59:26 +0200

shopt -s expand_aliases
alias 2grep=grep

2grep func like someone wrote

with vars u better use arrays for arg separation
grep2=( grep )

"$grep2" and only if more than one elements are "${grep2[@]}"

quotes, are only needed where spaces or data to preserve is, eg multiple
parts

its imho no problem to have a grep2=foo and use $grep2 instead of "$grep2"
cause foo is simple and doesnt contain spaces etc

On Thu, Oct 14, 2021, 03:51 Khan Smith <khansmith@mail.com> wrote:

>
>    Sent: Thursday, October 14, 2021 at 12:55 AM
>    From: "Leonid Isaev (ifax)" <leonid.isaev@ifax.com>
>    To: help-bash@gnu.org
>    Subject: Re: Alias inside function gives command not found
>    On Thu, Oct 14, 2021 at 02:48:03AM +0200, Khan Smith wrote:
>    > I am in a bash function with the following code
>    >
>    > alias etgrep='/bin/grep'
>    > etgrep -r -l "${isufx[@]}" -e "$ptrn" -- "${fdir[@]}" | sed
>    > "${sta}~${stp}!d"
>    > unalias etgrep
>    >
>    > Though everything was valid, but running the function in giving me
>    >
>    > bash: etgrep: command not found
>    Per bash manual:
>    -----8<-----
>    ALIASES
>    ...
>    Aliases are expanded when a function definition is read, not when the
>    function is executed, because a function definition is itself a
>    command. As a
>    consequence, aliases defined in a function are not available until
>    after that
>    function is executed. To be safe, always put alias definitions on a
>    separate
>    line, and do not use alias in compound commands.
>    ...
>    ----->8-----
>    HTH,
>    --
>    Leonid Isaev
>
>
>    I might be complicating this too much.  Instead I can use shell vars
>
> GREP='/bin/grep'
>


reply via email to

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