2

IMPORTANT: do not use eval! (I learned this later..)

In a function, eval expands sleep to its alias, so I prevent the endless loop this way:

function FUNCexecEcho() { 
  echo "EXEC: $@";
  shopt -u expand_aliases
  eval "$@";
  shopt -s expand_aliases
};
alias sleep='FUNCexecEcho sleep ';
sleep 10

But then, all other aliases will stop working...
How to prevent expansion only to one arbitrary alias inside this function?

PS.: the endless loop only happens on the second time you execute sleep 10

2

3 Answers 3

3

Use a backslash before the command you do not need an alias for.

For example: I use \ls when I do not want to invoke aliased version of ls which is ls -thor.

In the case of eval, you will need a double backslash, eg. eval \\ls.

2
3

You can also use the command, command. Like so:

$ alias echo="command echo 'hi: '"
$ echo "blah"
hi:  blah

But I can bypass the alias echo like so:

$ command echo "blah"
blah

Notice I actually used command in my alias definition as well. This avoids the alias from looping back on itself.

1

Using eval is wrong in the first place. The shell has already evaluated what you pass to FUNCexecEcho, evaluating a second time is wrong and potentially dangerous. In your code, you're also discarding the exit status of the command.

FUNCexecEcho() { 
  echo "EXEC: $@"
  "$@"
}

(no problem with aliases there unless you define an alias for "$@"). Compare the behaviour in:

FUNCexecEcho echo 'this;rm -rf "$HOME"'

with the two versions. With mine, it gives:

$ FUNCexecEcho echo 'this;rm -rf "$HOME"'
EXEC: echo this;rm -rf "$HOME"
this;rm -rf "$HOME"

I suggest you don't run it with yours if you don't have backups ;-)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .