LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-03-2007, 09:28 PM   #1
exitsfunnel
Member
 
Registered: May 2003
Distribution: RedHat
Posts: 60

Rep: Reputation: 15
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
 
Old 10-03-2007, 10:21 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009
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.)
 
Old 10-04-2007, 03:50 PM   #3
exitsfunnel
Member
 
Registered: May 2003
Distribution: RedHat
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
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
 
Old 10-05-2007, 12:34 AM   #4
freezykid
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Rep: Reputation: 0
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
 
Old 10-05-2007, 12:38 AM   #5
freezykid
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Rep: Reputation: 0
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...
 
Old 10-05-2007, 01:19 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
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.
 
Old 10-05-2007, 04:31 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009Reputation: 2009
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.
 
Old 10-06-2007, 08:26 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
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.

Last edited by jschiwal; 10-06-2007 at 08:29 PM.
 
Old 07-25-2014, 04:25 PM   #9
Lucas Malor
LQ Newbie
 
Registered: May 2013
Posts: 4

Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
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)
 
Old 07-29-2014, 09:12 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,616
Blog Entries: 34

Rep: Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
set a Global shortcut for my Application? mtux Programming 2 08-23-2007 02:47 PM
Where is global TERM variable set? tonyfreeman Red Hat 1 02-20-2007 02:20 PM
How make a script in order to set global alias? RickHunter_Ve Linux - Newbie 7 06-08-2004 02:13 PM
where to put global alias' true_atlantis Slackware 4 12-25-2003 02:31 PM
how do i set global prefs for bash polonius19 Slackware 2 03-06-2003 05:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration