5

I am looking for a simple way to create a permanent alias for all users. So ~/.bashrc or ~/.bash_profile is not an option.

Hasn't anybody created a program for this? I think it should be a very common need. If not, I can always create a custom Bash script, but I need to know if there is a equivalent of .bash_profile for all users.

In my case, I am using Mac OS X v10.9 (Mavericks) and Ubuntu 12.04 (Precise Pangolin), but I would like a method that works on major Unix systems.

UPDATE: I was wondering about a program which automatically allows the users to manage a list of permanent of aliases directly from the command-line without having to edit files. It would have options for setting for all users, target users, interative/login shell, etc.

UPDATE 2: Reply to answer of @jimmij

$ su -m
Password:
# cat /etc/profile
alias test343="echo working"
# cat /etc/bash.bashrc
alias test727="echo working"
# test727
bash: test727: command not found
# test343
bash: test343: command not found
Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
NeDark
  • 153
  • 1
  • 1
  • 5

2 Answers2

9

Please have a look at bash manual:

  • /etc/profile

    The systemwide initialization file, executed for interactive login shells

  • /etc/bash.bashrc

    The systemwide initialization file, executed for interactive, non-login shells.

  • ~/.bash_profile

    The personal initialization file, executed for interactive login shells

  • ~/.bashrc

    The individual per-interactive-shell startup file

  • ~/.bash_logout

    The individual login shell cleanup file, executed when a login shell exits

So you need to put your aliases in /etc/profile or /etc/bash.bashrc in order to make them available for all users.

terdon
  • 234,489
  • 66
  • 447
  • 667
jimmij
  • 46,064
  • 19
  • 123
  • 136
  • @terdon where did you get this `/etc/bash.bashrc` from? My manual of `GNU bash, version 4.2.53(1)` doesn't say anything about it. – jimmij Feb 07 '15 at 15:14
  • 2
    I've always known about it as the global init file, it's been around for as long as I've been using bash. It is also mentioned in the man page for my `bash 4.3.25`. That's where you define global stuff for interactive, non-login shells. Are you saying that `man bash | grep -F bash.bashrc` returns nothing on your system? – terdon Feb 07 '15 at 15:17
  • @terdon that is correct, I'm right now in front of recent *stable* bash on gentoo box, but there are indeed more recent unstable versions up to `4.3.33` – jimmij Feb 07 '15 at 15:44
  • @terdon At least on OS X yes, version 3.2.51(1) – NeDark Feb 07 '15 at 15:45
  • @jimmij Ubuntu 10.04's bash (4.1.something) does use it. See http://manpages.ubuntu.com/manpages/lucid/en/man1/bash.1.html. Also [Debian 6's](http://manpages.debian.org/cgi-bin/man.cgi?query=bash&apropos=0&sektion=1&manpath=Debian+6.0+squeeze&format=html&locale=en) – muru Feb 07 '15 at 15:53
  • Not working, details in question – NeDark Feb 07 '15 at 16:00
  • 1
    @NeDark Did you tried to with `su -`? `su -m` preserves environment, it is possible that it doesn't parse rc files. – jimmij Feb 07 '15 at 16:14
  • @jimmij Ok, that was the problem :) Working now thanks – NeDark Feb 07 '15 at 16:23
  • I checked the [bash manual](http://www.gnu.org/software/bash/manual/bashref.html) and found no mention of it! Still, I have been using it for years, it has been present on all systems I've ever used and it _is_ mentioned in my `man bash`. Something like that is needed though since non-login shells will not read `/etc/profile` or `/etc/bash.profile`. – terdon Feb 07 '15 at 16:33
  • Additionally, you may substitute aliases with commands (executables) which could be written as shell scripts: `/usr/local/bin/test727` would contain `#!/usr/bin/bash\n echo "working";`. This way, all users will have access to that command. – sleblanc Feb 07 '15 at 20:11
1

If you have ruby installed, you can use aka to generate permanent alias on the fly.

ytbryan
  • 131
  • 2