5

I am completely new to UNIX, so pardon me if this question is incredibly stupid.

I just started an internship, where I'm given access to a build server. I want to include certain aliases by default in my environment when I log in. From what I understand, the way to do it would be to include them in a .bashrc file in my $HOME directory. But it doesn't seem to work. I read somewhere that only interactive shells and user scripts would be able to read it, is that the reason it doesn't work? I'm running Bash. So how would I go about doing this?

Thanks in advance!

1
  • Only interactive shells read .bashrc, but that shouldn't be an issue because you're using an interactive shell, right? What did you try so far?
    – David Z
    Jul 12, 2010 at 21:57

4 Answers 4

3

iman453, the files need to be named .bashrc and .bash_profile. The period before the file means it is hidden. Any idea what version of Unix or Linux the build server is running?

In my home directory I have

.
|-- .bash_history
|-- .bash_logout
|-- .bash_profile
|-- .bashrc
|-- .mozilla
|   |-- extensions
|   `-- plugins
|-- .ssh
|   |-- .config
|   |-- authorized_keys
|   |-- authorized_keys2
|   `-- known_hosts
|-- .viminfo
|-- .vimrc

The contents of my .bashrc file are:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
alias chknag='sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'
alias ducks='sudo du -cksh * | sort -n | head -50'

The contents of .bash_profile:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/sbin:/usr/bin:/sbin

export PATH

When I SSH into this machine after I authenticate I have access to the alias ducks.

1
  • Oh, changing my bash profile to mimic yours worked! Thanks :)
    – iman453
    Jul 13, 2010 at 21:36
4

In your ${HOME}/.bash_profile, add the following:

# source the users bashrc if it exists
if [ -e "${HOME}/.bashrc" ] ; then
  source "${HOME}/.bashrc"
fi
2

From man bash:

When bash is invoked as an interactive login shell, or as a non-inter‐ active shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

and

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.

If, for example, you're logging in through a graphical user interface and then starting a terminal which is running Bash, then you're in an "interactive shell that is not a login shell".

1

You have to call .bashrc from your bash_profile. Refer here for Execution sequence for interactive login shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .