6

I created a this file: /etc/profile.d/pycharm_alias.sh

startPyCharm() {...

}

alias py=startPyCharm

I rebooted the PC, but then the alias is not available:

===> alias
...empty

If I login to the PC via ssh the alias is available:

===> ssh root@localhost
..
root@pc:~# alias
alias py='startPyCharm'

How to make an alias available for all users of a ubuntu linux system?

We use the bash shell.

  • You have answered your own question, and in the answer you seem satisfied. Please explain what you want beyond the answer of that solution? For example, do you want to know where to put the shellscript? And how to do it? – sudodus Dec 16 '19 at 12:50
  • @sudodus I found a work-around (don't use an alias), but no real solution. The question is still answered: How to make an alias available for all users of a ubuntu linux system? Fiddling with /etc/bash.bashrc is error-prone since I would like to avoid that several tools change one file. – guettli Dec 16 '19 at 13:14
  • 1
    Thanks for this explanation :-) Honestly, I think that aliases are intended for personal use (and should reside in each user's ~/.bashrc file). I'll try to find a method to make it work to globally change an alias (but I cannot see a way right now). I think using a shellscript and put it in a directory, that is in "everybody's path" is a good way to do it. So I think your answer really contains a good solution, and not 'just a workaround'. For this reason I will upvote it ;-) – sudodus Dec 16 '19 at 13:33
  • 1
2

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.

| improve this answer | |
  • Does creating a file in /etc/profile.d/ work for you? – guettli Dec 9 '19 at 14:25
  • I use /etc/skel which is copied into the user's home directory when they are created. This would not work for existing users – stratus Dec 9 '19 at 14:32
  • I know /etc/skel, but sometimes you want to change something after a user was created. – guettli Dec 9 '19 at 14:40
  • 1
    I would like to avoid changing /etc/bash.bashrc since it is error-prone if several tools change this single file. – guettli Dec 16 '19 at 13:15
1

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.

| improve this answer | |
1

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

| improve this answer | |
  • +1: good explanation :-) – sudodus Dec 16 '19 at 19:25
0

Why do you want an alias for all users?

Why not create a executable script?

A script is simpler and can be used from all shell, not just bash.

At least in my case using a script instead of an alias solved my issue.

BTW: This is just a work-around, not a real solution to the question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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