bash problem

Hello:

I add the following to my .bashrc:

alias pymol=/soft/pymol-1.8/pymol

after source the .bashrc file, I type:

which pymol

in the terminal, it said

which: no pymol in (/home/albert/install/autodock/ISM/bin:/home/albert/install/autodock/bin:/home/albert/install/rosetta_2014.35/main/source/bin:/home/albert/install/autogrow/LigBuilderV2/bin/:/usr/local/bin/:/usr/bin)

However, I noticed that csh has no such problem. I am just wondering can we somehow tell bash take the alias into account when we use “which” command?

thx a lot

Use the *type *command for aliases.

$ type -a ll
ll is aliased to `ls -l'

As @jsevans says.

Next time better first look t the man page

man which

which says

DESCRIPTION

Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1).

Thus no mentioning of taking aliases into account.

but it works in tcsh:

which pymol
pymol:   aliased to /soft/pymol/pymol

It only means that’s the problem of bash…

Again, why let you me do the searching in the man pages instead of doing it yourself?

From

man tcsh

under REFERENCE > Builtin commands

which command (+)
Displays the command that will be executed by the shell after substitutions, path searching, etc. The builtin command is just like which(1), but it correctly reports tcsh aliases and builtins and is 10 to 100 times faster. See also the which-command editor command.

(the bold emphasize is mine).

Thus the tcsh builtin is NOT the same as the which command.

And yes, there are different shells because they have different features and act different.

Thus, contrary to your conclusion that “it works in tcsh”, my conclusion is that both not only “work”, but that both even work to specification. And that when you see a problem here, that problem is probably your’s. :wink:

And I do like this one because it uses which to prove that which is not which:

henk@boven:~> which which
/usr/bin/which
henk@boven:~> exec tcsh
/home/henk> which which
which: shell built-in command.
/home/henk>

Hi,

There is no problem with bash because it is POSIX compliant shell and any shell that complies with posix standards has no use for which imho because which is an external command, meaning it is not built in to the shell that you’re using. An external commands/executable does not know anything about builtins executables, a common example of a builitin is the cd command/utility, now look up cd using which in bash.

which cd

The output would be something like

 which: no cd in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games) 

now switch to tcsh by the way no need to use exec just type tcsh

 tcsh 
 which cd 

There was a need because I wanted to replace my bash session by a tcsh session. I did not want to execute tcsh within my bash session.

Looking at the case of which we have in this thread and then comparing it with cd, we must take into account that cd is a bit a different beast because in both shells it is a builtin. For the simple reason that a separate tool cd(1) would be a rather useless beast.

Hi,

I just made cd as an example because it is a builtin and to be more precise, builitins function and aliases are those things that which does not know about in any POSIX shell. I’m not comparing which to cd just to be clear. Here is another example and it will be grep.

which grep

The out put is

/usr/bin/grep

Now using the builtin type

type -a grep

The output is


grep is aliased to `grep --color=auto'
grep is /usr/bin/grep
grep is /bin/grep
grep is /usr/bin/X11/grep

That is why greps output is always colored because the alias always takes the upper hand during command execution and which does not know that.

About the exec part, I’m not saying don’t use it nor I have any knowledge about what you’re trying to do using exec all I’m saying is that exec or not that should be the same result and exec happens to be a builtin in a posix shell.

which exec
which: no exec in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)

Also about cd although there is a man cd(1) that does not mean there is an external (to the posix shell) cd in your system. cd should be internal because you cannot change directory and remain in that directory using a script without using source a.k.a the dot . command and get this is it is also a builitin.

which source
which: no source in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)
which .
which: no . in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)

Now try using type :wink:

The bottom line is don’t rely on which when you’re using a **POSIX **compliant shell as much as possible, but hey no one is forcing you…

AFAIK only Solaris system has an external cd but that will be another topic and speaking of topic I’m already off topic…