1

This question already has an answer here:

For some reason, I had to add an alias to my bash. I have added the alias in /root/.bashrc which also contains the following piece of code:

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

The new alias works like a charm when it is used in the console. However, when I execute a script file myscript.sh with this alias in, I get a command not found error. It seems like I should tell .sh files to use the same environment as bash but I don't know how to do that.

marked as duplicate by muru, roaima, terdon bash Dec 16 '17 at 13:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from serverfault.com Dec 16 '17 at 11:12

This question came from our site for system and network administrators.

  • The solution here is to remove the problem. Aliasing Bash is not a good idea. – n.caillou Dec 16 '17 at 12:03
  • Note that you are only setting the alias for root. And root really has no business using aliases in scripts. You haven't given enough information to be sure, but it sounds like you are setting yourself up for a fall. I would strongly urge you not to use aliases in shell scripts that are run by root. – terdon Dec 16 '17 at 13:19
2

Aliases are not inherited by sub-processes. If you want the alias in your script, you could source in your /root/.bashrc again.. but, I wouldn't do that. I prefer to be more verbose in my scripts and use the actual command name.

I see aliases as being more of a keystroke saver in the shell. That isn't really necessary in a script. If anything, using the actual command, adds better understanding for anyone who may need to maintain your script later.

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