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

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



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


Reply to: