Defining aliases in bash

I'm trying to figure out what I'm doing wrong in my bash terminal window. I have a .bashrc file containing only the following 3 lines (for debug purposes):

echo " ******** Executing .bashrc on MickMac ********"
alias ls='ls -l'
echo " ******** Completed .bashrc on MickMac ********"

The .bashrc file certainly seems to run when I start up a terminal (I have the Terminal preferences set to run ~/.bashrc at startup), but strangely the alias command within it doesn't seem to take. However, if I subsequently manually source the .bashrc file then the alias does work. I'm sure I'm doing something stupid...

Here is the output of a terminal window right after freshly logging in:

Last login: Mon Jun 23 09:34:08 on ttys000
mueckmbp:~ mmueckti$ ~/.bashrc <------ done automatically
******** Executing .bashrc on MickMac ********
******** Completed .bashrc on MickMac ********
mueckmbp:~ mmueckti$ alias <------ my 1st command
mueckmbp:~ mmueckti$ source ~/.bashrc <------ my 2nd command
******** Executing .bashrc on MickMac ********
******** Completed .bashrc on MickMac ********
mueckmbp:~ mmueckti$ alias <------ my 3rd command
alias ls='ls -l'
mueckmbp:~ mmueckti$ ls -l ~/.bashrc <------ my 4th command
-rwxrwxrwx@ 1 mmueckti 33024 887 Jun 23 09:38 /Users/mmueckti/.bashrc
mueckmbp:~ mmueckti$


Annoyingly, if I also type 'man alias' into the terminal window I don't really get any information on the alias command!

Could some kind person please point out what I'm doing wrong.

Thanks,

Mick

17" MacBook Pro C2D, Mac OS X (10.5)

Posted on Jun 24, 2008 7:28 AM

Reply
6 replies

Jun 24, 2008 7:37 AM in response to Mick Mueck1

I'm not sure why it doesn't work. i suspect that .bashrc doesn't actually run but I would recommend the following.

According to 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 com-+
+mands 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. The --noprofile option may be used when the+
+shell is started to inhibit this behavior.+

therefore, add your commands to, say, ~/.bash_profile and it will run when you start bash for sure.

Jun 24, 2008 8:40 AM in response to V.K.

I get a slightly different MANPATH:

mueckmbp:~ mmueckti$ echo $MANPATH
/usr/share/man:/usr/local/share/man:/usr/X11/share/man

When I do 'man alias' the first few lines of what I get is:


BUILTIN(1) BSD General Commands Manual BUILTIN(1)

NAME
builtin, !, %, ., :, @, {, }, alias, alloc, bg, bind, bindkey, break, breaksw, builtins, case, cd,
chdir, command, complete, continue, default, dirs, do, done, echo, echotc, elif, else, end, endif,
endsw, esac, eval, exec, exit, export, false, fc, fg, filetest, fi, for, foreach, getopts, glob, goto,
hash, hashstat, history, hup, if, jobid, jobs, kill, limit, local, log, login, logout, ls-F, nice,
nohup, notify, onintr, popd, printenv, pushd, pwd, read, readonly, rehash, repeat, return, sched, set,
setenv, settc, setty, setvar, shift, source, stop, suspend, switch, telltc, test, then, time, times,
trap, true, type, ulimit, umask, unalias, uncomplete, unhash, unlimit, unset, unsetenv, until, wait,
where, which, while -- shell built-in commands

SYNOPSIS
builtin [-options] [args ...]

DESCRIPTION
Shell builtin commands are commands that can be executed within the running shell's process. Note
that, in the case of csh(1) builtin commands, the command is executed in a subshell if it occurs as any
component of a pipeline except the last.

If a command specified to the shell contains a slash ``/'', the shell will not execute a builtin com-
mand, even if the last component of the specified command matches the name of a builtin command. Thus,
while specifying ``echo'' causes a builtin command to be executed under shells that support the echo
builtin command, specifying ``/bin/echo'' or ``./echo'' does not.

Jun 24, 2008 10:45 AM in response to V.K.

Back to the question about why running the .bashrc script in the terminal shell. You have to source it (. ~/.bashrc in some shells) for your current environment to take on those values. If you just run the script it starts a new shell which would get that environment, but then goes away once the script completes.

Ultimately the renaming to .bash_profile was what you wanted to do, just offering the explanation for why just running it did not work.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Defining aliases in bash

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.