Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When trying to add a bit more detail to a related question, I realized I don't know how to specify which one of the available Bash word types to execute. According to help type there are five word types:

  • alias
  • keyword
  • function
  • builtin
  • file

It's possible for a word to have several types (true is just an example; a more commonly overridden word would be cd):

$ type -a true
true is a shell builtin
true is /bin/true

How do you force the execution of a specific synonym? So far, in order of precedence:

  • alias: This is a special case since it has precedence over all the other synonyms. Forcing this would only be useful when the command should fail if the alias is not defined.

    $ 
    
  • keyword:

    $ 
    
  • function:

    $ 
    
  • builtin:

    $ builtin true
    
  • file:

    $ command true
    $ $(which true)
    $ /bin/true
    

Partial solutions:

  • Quoting a word excludes aliases and keywords. That is,

    $ 'true'
    

    will run only the function, builtin or file.

share|improve this question
add comment

2 Answers

What you're asking doesn't really make much sense.

keyword means it's a word that is part of the syntax of the shell. Those are recognised through tokenising. Quoting them is enough for the shell to stop recognising.

It is possible to alias a keyword in most shells though. So, aliases have precedence over keywords (in effect they are expanded early, and there is more tokenising after that where you can have more aliases, keywords)...

aliases (with the exception of zsh aliases defined with alias -g are only expanded in command position), so typically not in builtin the-alias.

functions have precedence over builtins, and builtins over external commands (and then $PATH decides which one to use).

You can force the builtin with:

builtin the-cmd and its args

(though it should be noted it is not a standard command).

You can disable aliases by quoting them (though the quoted version could also be aliased in some shells).

So, for instance (here zsh syntax), in:

'while'() echo function while
alias 'while=echo alias while'

Writing while true would actually output alias while true, and there would be no way to use the while keyword since quoting it would disable both the alias and keyword.

You would call the while function with for instance:

'whi'le whatever

If there was a while builtin (but of course their wouldn't in those shells that have while keyword), you'd write it:

builtin while whatever

And to call the while command, you'd write:

command while whatever

Or

/full/path/to/while whatever

Of course, nothing's stopping you from doing even sillier things like:

alias 'while=while "w"hile; do'
"while"() { while "whi"le; done; }

Which, at least in zsh is valid (but stupid).

share|improve this answer
    
Quoting suppresses alias lookup, and command suppresses function lookup, but neither of these specify which of the remaining synonyms will be used. I'm looking for a way to specify to execute only a specific synonym. –  l0b0 Jan 22 '13 at 13:50
    
@l0b0 "command" suppresses all of alias, keyword and builtin. –  Stephane Chazelas Jan 22 '13 at 13:55
    
As for making sense, builtin is used in for example RVM to override cd for a nice effect. –  l0b0 Jan 22 '13 at 13:55
    
You're right; and I just verified that it also suppresses functions. +1. –  l0b0 Jan 22 '13 at 13:57
add comment

An extremely useful idiom for me is this:

FALSE=1; TRUE=0
DEVEL_FLAG=$FALSE
...
in_development() { return $DEVEL_FLAG ; }
cd()
{
 if in_development ; then # -D argument to script toggles development flag
   echo "Would run cd $*"
  else
   builtin cd "$@"
 fi
}

Now if I run the script with -D specified, development mode is activated, and the cd (or any other wrapped command) echoes debug info

This is especially useful with scripts which run ssh. Sure I could toggle set -x, but this is more useful and readable, for me.

share|improve this answer
    
How does this answer the question? –  l0b0 Sep 7 '13 at 8:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.