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

Re: Re: Bash argument expanded inside alias



On Tue 13 Mar 2012 15:49:52 +0000(+0000), Clive Standbridge wrote:
> Oh I think I misunderstood the question.
> On second thoughts, I think you probably meant to use an alias inside
> a  shell script, and to use the script's argument in the alias. In that
> case, the problem would be shell parameters are not expanded inside
> single quotes. You would need to use double quotes, for example
>
> alias muda="find . -name '*' -mtime -$1"
>
>
> --
> Cheers,
> Clive

Hi Clive,

Just for the record, I tried to include in ~./bash_aliases the
following command:

alias muda="find . -mtime -$1"

(As Chris Davies correctly suggested I don't need -name "*", so I simplified it)

But after run

$source ~./bash_aliases
$alias
alias muda='find . -mtime -'

Funny thing, I realized and run:
$source ~./bash_aliases 10
$alias
alias muda='find . -mtime -10'

Finally I came to the conclusion I really can't use args inside alias,
as you stated in an earlier message. All other answers to this email
suggesting $1 inside an alias fail because of that. The alias is
created using the script arguments, not the arguments passed to the
alias during its usage.


My last solution, very interesting, is that I don't need an argument
at all. See what works! Inside ~./bash_aliases I wrote:

alias muda="find . -mtime "

Now, after run
$source ~./bash_aliases
$alias
alias muda='find . -mtime '

And I can run my alias without using a function, just typing:
$ muda -10

It will concatenate the whole alias with the arg and run.

Thats a nice solution without using any function at all. Also, it
explains why you changing the -$1 to $1 (passing a negative value,
instead of keeping the 'minus' in the alias and passing a positive
value) works. Actually, all $1 is substituted to an empty string, and
it is just like the alias above.

Unfortunately, you cannot, for example, change the directory with an
argument, because 'find' can't accept a command in an inverted order
like:
$find -mtime -10 /home/kilemal
in case which your alias could be just alias muda='find -mtime'. You
need a function to achieve that.


Thanks all for your help and patience,

--
Beco
Linux Counter #201942
The man who revived a pentium 200 MMX from 1997, with debian squeeze
and made a server with it!


Reply to: