0

I am adding some aliases to .bashrc to discourage people from using standard commands where there is a more complex process involved in achieving the desired result. But how can I set the exit code in case the alias is invoked from a script? Ideally without generating a spurious error message like "no such file or directory" (I did find this, but surely there a cleaner way?)

e.g.

 alias useradd='echo "Nope....you should be using the custom script."'

should result in.....

 # useradd newuser
 Nope....you should be using the custom script.
 # echo $?
 -1
 #
symcbean
  • 5,540

1 Answers1

6

You should use a function instead of an alias.

useradd () {
  echo "Nope....you should be using the custom script." >&2
  return 2
}

See In Bash, when to alias, when to script, and when to write a function? for more information on the limitations of aliases and usefulness of functions.

Also note that whether you use a function or an alias your users can bypass them by using the command builtin or simply escaping the command name like:

\useradd ...
jesse_b
  • 37,005