RE: Unnecessary rebooting (was Re: Bash argument expanded inside alias)

March 14th, 2012 - 08:10 am ET by rcb | Report spam

From: Chris Bannister <cbannister@slingshot.co.nz>, Date: Wed, 14 Mar 2012 18:00:24 +1300

[Please, don't top post in this mailing list]

Just logout then log back in again, or even better, type:
"source .bashrc" at a command prompt. :)
"Religion is excellent stuff for keeping common people quiet."
  -- Napoleon Bonaparte




Dear Chris,

Thanks for this great tip "source". I must admit I google for
something like that, but I was trying to find in "man bash" some
argument to let bash or sh to run in the same environment. I think its
an old trick, and 'source' is newer, but I'm not sure and I didn't
find anything.



Dear Camaleón,

Unfortunately, this nice command of yours doesn't seem to work. Is it
me typing something wrong, or there is really a problem?
:~$ alias muda='find $1 -name "*" -mtime $2'
:~$ muda /home/cheetara -4
find: invalid arg `-4' for `-mtime'

Funny is that no matter if I change $2 to $1, the error is the same. Look:
:~$ alias muda='find $2 -name "*" -mtime $1'
:~$ muda /home/aluno -4
find: invalid arg `-4' for `-mtime'

Even worse, I tell it to use $1, but give two args in the command line, and get:
:~$ alias muda='find . -name "*" -mtime $1'
:~$ muda /home/cruelbash -4
find: invalid arg `-4' for `-mtime'


That is just to chitchat, as for now the function solution works great!

Thanks,
Beco.

PS. Sorry for an earlier "top post", and an warning of "non html".
It'll not happen again.
PPS. I think I need to change the list subscription from digest to
normal emails. I tried subject:help to debian-user-request, but the
message that came back was of no help. (Maybe this is offtopic here --
in this email of course, and in the list in general)? A quick
indication will do.


To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: http://lists.debian.org/CALuYw2wS1C...vsMQTm0mrA@mail.gmail.com
email Follow the discussionReplies 2 repliesReplies Make a reply

Replies

#1 Dom
March 14th, 2012 - 10:00 am ET | Report spam
On 14/03/12 12:50, rcb wrote:
Dear Camaleón,

Unfortunately, this nice command of yours doesn't seem to work. Is it
me typing something wrong, or there is really a problem?
:~$ alias muda='find $1 -name "*" -mtime $2'
:~$ muda /home/cheetara -4
find: invalid arg `-4' for `-mtime'



Command line paramaters aren't passed to the alias as arguments in $1,
$2 etc - those will expand to "". Instead your parameters are being
added to the end of the aliased command, giving you:

find -name "*" -mtime /home/cheetara -4

for both this and the following form:

Funny is that no matter if I change $2 to $1, the error is the same. Look:
:~$ alias muda='find $2 -name "*" -mtime $1'
:~$ muda /home/aluno -4
find: invalid arg `-4' for `-mtime'



You can get a better idea of what is happening by using "set -x" before
running your muda command, and "set +x" afterwards. The expanded aliases
will be displayed when they are executed.

Quoting the bash man page section on aliases:

"There is no mechanism for using arguments in the replacement text. If
arguments are needed, a shell function should be used"

That is just to chitchat, as for now the function solution works great!



Good to hear you got a result :-)

Dom


To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Archive: http://lists.debian.org/

Similar topics