1

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.

Nate
  • 121
  • 4
  • 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

1 Answers1

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.

Erik
  • 165
  • 5