[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Alias inside function gives command not found
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: Alias inside function gives command not found |
Date: |
Thu, 14 Oct 2021 08:32:21 +0200 |
On Thu, Oct 14, 2021 at 03:01:10AM +0200, Khan Smith wrote:
> Sent: Thursday, October 14, 2021 at 12:55 AM
> From: "Seth David Schoen" <schoen@loyalty.org>
> To: "Khan Smith" <khansmith@mail.com>
> Cc: "help-bash" <help-bash@gnu.org>
> Subject: Re: Alias inside function gives command not found
> Khan Smith writes:
> > 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
> I'm not sure why this is so in terms of bash scoping rules, but you can
> make etgrep itself a function, like
> etgrep() { /bin/grep "$@"; };
> at which point you can use it inside of another function (including if
> it was defined there). Instead of unalias, you can remove the function
> binding with "unset etgrep". This should probably work as well for most
> purposes.
>
>
> I was doing this because I have an alias for grep that uses colour.
> But
> when I use grep, the file matches include the colour codes. This makes
> problems when piping the grep results to ather commands.
I'm assuming you have an alias that uses GNU grep with "--color=always",
right? Change that alias to use "--color=auto" instead. This should
make GNU grep only use color when its standard output stream is a
terminal. Alternatively, use "command grep" or "grep --color=never" in
your function.
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.