skip to Main Content

So I’m trying to create an alias to run two python scripts when a new bash window is opened but I keep running into odd issues. If I create the alias manually in a terminal window, no problem:

alias UserPasswordReset="python3 /bin/Admin.py"
alias SystemFileCheck="python3 /bin/SystemFileCheck.py"

If I manually create a file called aliasfile.sh and put it in /etc/profile.d/ with of course #!/bin/bash as the first line, no problem when opening a new terminal the alias are there and work.

So here is the issue, if I pre-create the file and copy it into the /etc/profile.d/ directory I get strange results even though the contents of the file are exactly the same. If I chmod the file to the same as the other in there it makes no difference. This is the output I get

enter image description here

Not sure whats going on here or what I’m doing wrong, it should be such a simple thing to do.

2

Answers


  1. You have to use . aliasfile.sh to source the file. And also remove the shebgang line (#!/bin/bash) otherwise the alias will only be for the script.

    Login or Signup to reply.
  2. If you want the aliases be automatically available for every user, you have to add the definitions in some global initialization file which will automatically source.

    The problem here is that you need the aliases fo interactive shells, and the rules for interactive shells are a bit tricky. From the man-page:

    When bash is invoked as an interactive login shell, it first reads and executes commands
    from the file /etc/profile, if that file exists. When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

    Hence, you have to source your aliasfile.sh in your /etc/profile. But this covers only the first case. For instance, if a user opens a interactive non-login shell, he would still not see the definitions. Since you are not permitted to hack the user’s homedirectories, you better tell each use to also add a

    . /path/to/setup/directory/aliasfile.sh
    

    in his ~/.bashrc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search