0

I have been trying to add optional aliases to my .bash_aliases file, but I have run into an issue with optionally adding aliases. So far it seems that the aliases get added no matter what the if statement seems to be evaluating to true.

Here is an example of what I am trying to do:

alias gh="history | grep"
alias python="python3"

# personal computer aliases
if [ ${COMPUTER_TYPE} = "personal" ]
then
  alias hibernate="sudo systemctl hibernate"
fi

Here is the variable being set in .bashrc (setting it in .profile does not seem to do anything either):

export COMPUTER_TYPE=work

Echoing the variable:

$ echo $COMPUTER_TYPE
work

Any idea what I am doing wrong here? If I had to guess, the .bash_aliases file is not a bash file that gets treated as one, but that seems unlikely. Thanks for the help!

Edit: I see that my question may be misleading as the example used was a personal computer, but the problem is that when I run the alias on a work computer it still adds all aliases. I have since updated the example to use work instead of personal.

Peter Kaufman
  • 752
  • 1
  • 6
  • 18
  • When you source your .profile or .bash_profile are you also sourcing .bashrc and .bash_aliases? If so, in what order? – j_b Feb 15 '23 at 01:00
  • do you see the other aliases set in the file work? If so, then the `.bash_alias` is getting sourced correctly. Good luck. – shellter Feb 15 '23 at 01:16
  • 3
    You might think about deferring the `COMPUTER_TYPE` check until runtime instead of requiring it to be set correctly at definition time. You can do that by making `hibernate` a function instead of an alias: `hibernate() { case $COMPUTER_TYPE in personal) sudo systemctl hibernate "$@";; *) echo "Ignoring hibernate command on a non-personal computer" >&2; return 1;; esac; }` – Charles Duffy Feb 15 '23 at 02:00
  • I can say that the aliases are getting sourced correctly. It does seem the export was happening after the sourcing of the `.bash_aliases` file, but since it requires the value to be set to `personal` that should not matter given that the if statements are always being triggered. However I did try moving it to before the sourcing and that did not change the list of aliases. – Peter Kaufman Feb 15 '23 at 02:12
  • @PeterKaufman, what do you mean "the if statements are always being triggered"? With your current code, they're only triggered _at definition time_, not at expansion time, so `COMPUTER_TYPE` gets checked when `.bash_aliases` is deciding whether to define a `hibernate` alias, not later when you're trying to _use_ that alias. That's the whole point of the function I suggest above: it moves the check to runtime. – Charles Duffy Feb 15 '23 at 14:45
  • 1
    @PeterKaufman, one thing you might find useful is to test order-of-operations with `PS4=':${BASH_SOURCE}:$LINENO+' bash -xli` to get a log of everything your shell does at startup time. If you really did successfully cause `COMPUTER_TYPE` to be set before `.bash_aliases` is executed, you'll see it in that log; and by reproducing said log in your question, you'll be able to convince skeptics like me who don't believe you. :) – Charles Duffy Feb 15 '23 at 14:49
  • @CharlesDuffy, you are correct that it was not being set in the correct order initially, but since it was still adding the values and saying that an unset variable was equal to "personal" caused confusion. Even when the value was set on the line before `.bash_aliases` was sourced, it did not change the alias addition. However using the function version worked. – Peter Kaufman Feb 15 '23 at 16:57
  • I'd like to see the xtrace log for the alias not being created when the value is set on a prior line. – Charles Duffy Feb 15 '23 at 17:18

1 Answers1

1

Right now, you're checking COMPUTER_TYPE while your .bash_aliases file is being sourced, not when the hibernate command is being run. Consequently, you're creating an ordering dependency: .bash_aliases only defines hibernate at all when COMPUTER_TYPE is set at a prior point in initialization.

The easy way to avoid this dependency is to make hibernate be a function instead, so it gets defined either way but then checks COMPUTER_TYPE at runtime:

hibernate() {
  case $COMPUTER_TYPE in
    personal)
      sudo systemctl hibernate "$@"
      ;;
    *)
      echo "Ignoring hibernate command on a non-personal computer" >&2
      return 1
      ;;
  esac
}
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441