4

Using enable builtin command we can disable a shell builtin command, for example echo, as follow:

enable -n echo

But how can we disable a shell keyword like time? If we try:

enable -n time

we will get the following "answer":

bash: enable: time: not a shell builtin

Note: I know how to run the time that's in /usr/bin, so I just want to know if it is possible to accomplish what I asked in the title.

  • Anything you truly want to accomplish why you want to disable the time keyword? – konsolebox Aug 5 '14 at 11:28
  • @konsolebox time it was just an example. If you type help in your terminal using bash you can find out that A star (*) next to a name means that the command is disabled. That's what I want to accomplish. – Radu Rădeanu Aug 5 '14 at 11:33
  • 2
    I would guess that disabling a shell keyword is actually a means to some other end. What are you trying to accomplish by disabling a keyword? – Doug O'Neal Aug 5 '14 at 15:11
  • @DougO'Neal Let say that disabling a shell keyword allows you to execute a disk command which has the same name as a shell keyword without using a full pathname or an alias. – Radu Rădeanu Aug 5 '14 at 19:41
6

You can do:

alias time='"time" '

(the trailing space is to allow alias expansion after it, as a bonus).

Quoting a keyword (keywords being part of the shell language syntax) stops it from being interpreted as a keyword, so here, normal command look up is performed on it.

It works in bash, zsh, mksh, but not ksh93, ash or yash.

Note that for non-interactive bash instances (like in scripts), you need shopt -s expand_aliases, as contrary to other shells, bash doesn't expand aliases by default when not interactive.

  • You didn't read my note from my question (or you didn't understood it). If you type help in your terminal using bash you can find out that A star (*) next to a name means that the command is disabled. That's what I want to accomplish. – Radu Rădeanu Aug 5 '14 at 11:28
  • 1
    @RaduRădeanu, then help() { builtin help "$@" | sed 's/ time /*time /'; } – Stéphane Chazelas Aug 5 '14 at 11:33
  • I repeat: If you type help in your terminal using bash you can find out that A star (*) next to a name means that the command is disabled. That's what I want to accomplish. If this is not possible, then show some resources why... – Radu Rădeanu Aug 5 '14 at 11:51
  • 1
    @RaduRădeanu It is not possible to mark a keyword as disabled in bash because the notion of marking as disabled applies only to builtins, not to keywords. It is possible to disable a keyword, as in making the word not be a keyword anymore, and Stéphane has showed you how. – Gilles Aug 5 '14 at 21:44
  • 1
    Hmmm. POSIX states that “reserved words in correct grammatical context shall not be candidates for alias substitution”, which is a bit odd considering that alias expansion substitution “before applying the grammatical rules” and before recognizing reserved words. It looks like the spec was written specifically to accommodate some odd behavior from ksh. – Gilles Aug 5 '14 at 21:49
2

As you found out, you can't disable a keyword with enable -n.

However, you can make a keyword a builtin also with enable -f and then disable it:

  • unpack the sources for your current version of bash
  • ./configure && make
  • In examples/loadables, you'll find a number of example loadable builtins, edit one of them, for instance the sync one to replace all instances of sync with time.
  • run make in examples/loadables.
  • In bash, run enable -f ./sync time.
  • enable -n time now works. And help shows: *time (in addition to time [-p] pipeline)

(not that it's ever going to be useful to anyone).

  • 1
    Nice try and I admit that I was fooled until to check step by step your answer :). But the new created shell builtin time will not replace the shell keyword time and, the most important, will never work as you described (bash will crash immedialy after the new created builtin is runned). In the end enable -n time will work for new created builtin, but not for the shell keyword as I asked. So, in fact, your answer doesn't have nothing to do with my question. – Radu Rădeanu Aug 5 '14 at 18:54
  • @Radu, Yes, as I said in the initial version of the answer (before somebody edited that away), stupid answer to a stupid question. If you want to disable the keyword, see my other answer. – Stéphane Chazelas Aug 5 '14 at 19:18
1

Rather than disabling the keyword, if you can call it without the shell's parser finding it in command position - or the first word in a simple command - you should be able to run it without trouble. The POSIX-specified command command is designed to do just that. So you can, for instance, call a command called time like:

command time

This works in any shell as far as I can tell. It is strange, though, that many seem to handle it differently. Here's a demo:

echo 'echo "$0"' >./time
chmod +x ./time
for sh in dash ksh zsh bash yash 'busybox ash' posh mksh
do  command -p $sh -c '
    PATH=.:$PATH
    printf "\n%s\n" "$0"
    time
    command time' "$sh"
done 

OUTPUT

dash
./time
./time

ksh
user    0m0.00s
sys     0m0.00s
time

zsh
shell  0.00s user 0.00s system 71% cpu 0.005 total
children  0.00s user 0.00s system 0% cpu 0.005 total
time

bash

real    0m0.000s
user    0m0.000s
sys     0m0.000s
./time

yash
./time
./time

busybox ash
./time
./time

posh
./time
./time

mksh
    0m0.00s user     0m0.00s system
./time
0

In zsh you can disable reserved keywords with the -r flag.

-2

You can't disable it but you can hide it with a function. Use at own risk.

shell_builtin() {
    :  ## Do nothing.
}

To enable it back, simply unset it:

unset shell_builtin
  • 1
    The OP want to disable shell keyword, not builtin. – cuonglm Aug 5 '14 at 11:13
  • 1
    @Gnouc Then it's not possible. – konsolebox Aug 5 '14 at 11:14
  • You can use complete -r commandname. to remove a certain command from autocompletion. – holasz Aug 5 '14 at 11:21
  • 3
    @holasz I was quite hasty answering it. time is a special keyword that's probably on the level of if, while, case, etc. that's why you can't disable it. – konsolebox Aug 5 '14 at 11:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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