LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Where to set global alias? (https://www.linuxquestions.org/questions/linux-general-1/where-to-set-global-alias-589262/)

exitsfunnel 10-03-2007 09:28 PM

Where to set global alias?
 
Hi,

I'd like to make the alias 'll=ls -l' available to all users at all times. I've put it /etc/profile which works for console logins however I find that after launching X/Gnome and starting a gnome-terminal, the alias is gone. Is the issue that gnome-terminal creates a non login bash session? Is there some easy workaround here? Thanks.

-exits

David the H. 10-03-2007 10:21 PM

That's correct, to my understanding. /etc/profile is the configuration file for login shells, and doesn't affect regular bash terminals directly. The file for that is /etc/bashrc.

The best way to configure things, AIUI, is to put all of your aliases and other modifications into bashrc, and then set /etc/profile to import bashrc when callled. Simply place an entry like the following into /etc/profile, and the settings should work for both:

Code:

#import /etc/bashrc to this file

if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

(A single period in bash scripting means to import a file, so all this does is check to see if a file "/etc/bashrc" exists, and to import it if it finds one.)

exitsfunnel 10-04-2007 03:50 PM

Quote:

Originally Posted by David the H. (Post 2912516)
That's correct, to my understanding. /etc/profile is the configuration file for login shells, and doesn't affect regular bash terminals directly. The file for that is /etc/bashrc.

The best way to configure things, AIUI, is to put all of your aliases and other modifications into bashrc, and then set /etc/profile to import bashrc when callled. Simply place an entry like the following into /etc/profile, and the settings should work for both:

Code:

#import /etc/bashrc to this file

if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

(A single period in bash scripting means to import a file, so all this does is check to see if a file "/etc/bashrc" exists, and to import it if it finds one.)

Thanks David, for taking the time to reply. I'm not sure though that I understand the point of what you suggest. If the problem is that /etc/profile isn't being called, how does it help to move the alias into a file sourced in from /etc/profile? If /etc/profile isn't executed, neither will /etc/bashrc be.

-exits

freezykid 10-05-2007 12:34 AM

Edit your '.bashrc' file in your home directory and add the alias there.
Atleast thats what i have done to alias my clear to c (i am too lazy to type it all)

type in:
alias ll='ls -l'
in ur .bashrc and restart your terminal, voila things work the way u want...
Anything else

freezykid 10-05-2007 12:38 AM

you can do the same with the /etc/bashrc to make it available to all the users...
But you can do it only if you have root permissions...
Sorry did not read ur post fully...

jschiwal 10-05-2007 01:19 AM

Did you log out after modifying /etc/profile?


Generally, read through the startup files on your system. There usually is an aliases file that the system sources that would be the best place to put alias for all users.

David the H. 10-05-2007 04:31 AM

As I said, /etc/profiles is the login shell config file and /etc/bashrc is the non-login config file. Your alias needs to be present in both files in order to work everywhere.

Now, you can simply add the same line to both files and it will work fine, but this can get tedious when you have a lot of additions or change things a lot. A simpler way is make it so that /etc/profile imports the contents of /etc/bashrc when its read (using the 'if' statement I gave above). Then you only have to edit the one file and the contents will be available to both.

You could of course do it the other way around and import the contents of /etc/profile into /etc/bashrc, but since login shells often have different requirements than non-login ones, I don't think it's recommended.

You could even create a completely separate file and import that instead. As the last poster mentioned, there's a chance that there's already a separate aliases file set up for you. If your config has any import lines similar to the one I gave, see where it leads.

Finally, don't forget that there's also the ~/.bashrc, which is the user's bash config file. Bash reads it last, and will add to or override any global settings in /etc/bashrc. This lets each user have their own personalized aliases.

And yes, you do have to make sure you log out (of the shell you're using at least) and back in again in order to apply any changes to the config files. Optionally you can type in 'source /etc/bashrc' to import that file's settings into your current shell.

jschiwal 10-06-2007 08:26 PM

I think that you will get a login shell started when you first login so the ~/.profile command will be executed. An interactive shell you start will be a child of the login shell so it will inherit the environment. Also, /etc/profile will source a users ~/.bashrc file, so that will be executed when you first login as well (unless you mucked up /etc/profile).

Even given all this, most distro's will source an aliases file somewhere. IMHO, it is best to read through your systems startup scripts and learning where such things are done instead of automatically applying a generic solution. Especially if you want these aliases to be present for all users.

I would recommend creating an ~/.aliases file in your home directory and adding a line like
test -z "$HOME/.aliases" && . $HOME/.aliases
in either ~/.bashrc or ~/.profile. A file of only your aliases would be easier to maintain.

Lucas Malor 07-25-2014 04:25 PM

Quote:

Originally Posted by David the H. (Post 2912516)
That's correct, to my understanding. /etc/profile is the configuration file for login shells, and doesn't affect regular bash terminals directly. The file for that is /etc/bashrc.

Currently the correct file is /etc/bash.bashrc (under Debian and Debian-like, at least)

onebuck 07-29-2014 09:12 AM

Member Response
 
Hi,

Please do not resurrect 7 year old threads that are considered necro.

When using '~/.bashrc' one should use '~ /.bash_profile' to source the ~/.bashrc;
Code:

cat .bash_profile
# .bash_profile
#08-30-06 12:21 gws copied loki:/root

# Source .bashrc
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

To keep things clean for the user.

When you use '/etc/profile';
Code:

~# cat /etc/profile
# /etc/profile: This file contains system-wide defaults used by
# all Bourne (and related) shells.

By using the users home '~/.bash_profile to source the '~/.bashrc' you can keep things clean when a shell is opened;
Code:

#.bashrc
#08-30-06 12:20 gws copied loki:/root

# Add bin to path
export PATH="$PATH:$HOME/bin"

# Dynamic resizing
shopt -s checkwinsize
#
#save bash history so as to share

shopt -s histappend
PROMPT_COMMAND='history -a'

# Custom prompt
#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#08-29-06 11:40 gws

if [ `id -un` = root ]; then
  PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
 else
  PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
fi
#
# Add color
eval `dircolors -b`

#Terminus is a very nice Unicode font for the Linux console
#02-02-12 gws
#from dugan's site http://duganchen.ca/writings/slackware/fonts/

#04-30-12 11:41 removed
#
#if [ $TERM = "linux" ]; then
#    setfont ter-v16n
#fi

# User defined aliases
alias cls='clear'
alias clls='clear; ls'
alias ll='ls -l'
alias lsa='ls -A'
alias lsg='ls | grep'
alias lsp='ls -1 /var/log/packages/ > package-list'
alias na='nano'
alias web='links -g -download-dir ~/ www.google.com'

#08-29-06 11:50 gws

#To clean up and cover your tracks once you log off
#Depending on your version of BASH, you might have to use
# the other form of this command
  trap "rm -f ~$LOGNAME/.bash_history" 0

#The older KSH-style form
#  trap 0 rm -f ~$LOGNAME/.bash_history

Hope this helps.
Have fun & enjoy!
:hattip:


All times are GMT -5. The time now is 08:58 AM.