Results 1 to 10 of 10

Thread: bash problem

  1. #1

    Post bash problem

    Hello:

    I add the following to my .bashrc:

    Code:
    alias pymol=/soft/pymol-1.8/pymol
    after source the .bashrc file, I type:

    Code:
    which pymol
    in the terminal, it said

    Code:
    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

  2. #2
    Join Date
    Nov 2016
    Location
    Prague, CZ
    Posts
    39

    Default Re: bash problem

    Use the type command for aliases.

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

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,730

    Default Re: bash problem

    As @jsevans says.

    Next time better first look t the man page
    Code:
    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.
    Henk van Velden

  4. #4

    Default Re: bash problem

    but it works in tcsh:

    Code:
    which pymol
    pymol:   aliased to /soft/pymol/pymol
    It only means that's the problem of bash....



    Quote Originally Posted by hcvv View Post
    As @jsevans says.

    Next time better first look t the man page
    Code:
    man which
    which says

    Thus no mentioning of taking aliases into account.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,730

    Default Re: bash problem

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

    From
    Code:
    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.
    Henk van Velden

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,730

    Default Re: bash problem

    And I do like this one because it uses which to prove that which is not which:
    Code:
    henk@boven:~> which which
    /usr/bin/which
    henk@boven:~> exec tcsh
    /home/henk> which which
    which: shell built-in command.
    /home/henk>
    Henk van Velden

  7. #7

    Default Re: bash problem

    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.
    Code:
    which cd
    The output would be something like

    Code:
     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
    Code:
     tcsh
    Code:
     which cd
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,730

    Default Re: bash problem

    Quote Originally Posted by jetchisel View Post
    by the way no need to use exec just type tcsh
    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.
    Henk van Velden

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,730

    Default Re: bash problem

    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.
    Henk van Velden

  10. #10

    Default Re: bash problem

    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.

    Code:
    which grep
    The out put is
    Code:
    /usr/bin/grep
    Now using the builtin type
    Code:
    type -a grep
    The output is
    Code:
    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.

    Code:
    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.

    Code:
    which source
    which: no source in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)
    Code:
    which .
    which: no . in (/home/jetchisel/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)
    Now try using type

    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...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •