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

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 ;-)

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.