Solution 1:

You can try putting this in /etc/bash.bashrc. It's stated purpose is # System-wide .bashrc file for interactive bash(1) shells. Which seems like it may be what you want. I am surprised that your profile.d solution did not work.

Solution 2:

Aliases are personal tools

  • I think aliases are intended for personal use (and should reside in each user's ~/.bashrc file).

Shellscripts (and other programs) are general tools

  • Make a shellscript and put it in a directory, that is in "everybody's path".

    • But it is important to be careful in order to avoid overwriting existing programs or putting existing programs in shadow. You may put the shellscript before an existing program in path, so that the system will not find it via PATH.

      In other words, make sure that the name is unique to avoid problems with other programs.

Detailed tips

This is my path in Lubuntu 18.04.x LTS.

$ echo $PATH
/home/sudodus/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/games

I have an own bin directory, which is automatically found and put in the beginning of the path (with highest priority). But the rest of the path is standard.

I would suggest that you

  • check that there should be no conflict with the name

    type unique-name  # shows if it exists and what kind of program it is
    which unique-name # shows where an installed program is stored 
    unique-name       # if known but not installed, you get a hint about it
    apt-cache policy *unique-name*  # package name (may or may not be same as program name)
    
  • if no conflict, create a shellscript (this is a trivial example)

    echo 'echo "Hello World"' > unique-name
    
  • make the shellscript executable

    chmod +x unique-name
    
  • put the shellscript into /usr/local/sbin if it needs root privileges or otherwise into /usr/local/bin.

    sudo cp -i unique-name /usr/local/bin
    

    The option -i prompts you if the name already exists in the target directory.

How all users can run your shellscript

When you make the shellscript executable and it is in a directory in everybody's PATH, everybody can run it via its file name,

unique-name

There are several short strings that are not yet used as names for standard programs, and you can find such names by testing with

type short-string-to-be-tested

for example

$ type py
bash: type: py: not found

In my computer there is no executable program and no shell built-in with that name, so I can use py as a file name (and I need no alias).

But if you try to run py before renaming your shellscript to that name,

$ py

Command 'py' not found, but can be installed with:

sudo apt install pythonpy

you will find that there is such a program (but it is not yet installed), and it might be a good idea to select another name, for example

pych

which can be a short name derived from your original name PyCharm.

Solution 3:

Why the global alias deosn't work

One reason the global alias doesn't work is described here:

The method in this answer should not be used. Aliases in .sh files in /etc/profile.d/ (or /etc/profile) will be defined only for login shells and they will not work in interactive non-login shells. Unlike environment variables, bash can't export aliases to child processes, not even child bash shells. This method may seem to work properly if it's only tested in login shells, such as the original shell obtained by logging on in a virtual console or via SSH, but it fails in their child shells and also fails in shells started by GUI terminal windows. – Eliah Kagan Apr 18 '15 at 0:56


Use a global function in /etc/profile.d

Currently you have /etc/profile.d/pycharm_alias.sh containing:

startPyCharm() {...

}

alias py=startPyCharm

Assigning an global alias to a function isn't necesssary. You could simply create a global function called py that does what StartPyCharm does.

For example, create /etc/profile.d/py.sh containing:

#!/bin/bash

py () {
    echo "Running py function in /etc/profile.d/py.sh"
    echo "Substitute for py alias which calls StartPyCharm function"
}

export -f py

Replace the echo commands with the contents of your current StartPyCharm () function.

When a user logs in and opens the shell they can type py and a global function is called instead of a global alias to a function.

To see all your functions (both global and local) use declare -F