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:
env while whatever
or in zsh
(when not in sh
emulation):
command while whatever
(in other shells, command
only prevents functions, not builtins)
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).
I knew I was grasping at straws, but UNIX never fails!
Here's how I managed it:
bash$ gdb --pid 8909
...
Loaded symbols for /lib/i386-linux-gnu/i686/cmov/libnss_files.so.2
0xb76e7424 in __kernel_vsyscall ()
Then at the (gdb)
prompt I ran the command, call write_history("/tmp/foo")
which will write this history to the file /tmp/foo
.
(gdb) call write_history("/tmp/foo")
$1 = 0
I then detach from the process.
(gdb) detach
Detaching from program: /bin/bash, process 8909
And quit gdb
.
(gdb) q
And sure enough...
bash$ tail -1 /tmp/foo
while true ; do echo 1 ; echo 2>/dev/null ; sleep 30 ; done
For easy future re-use, I wrote a bash script, automating the process.
Best Answer
Use
set -x
in the shell.Using
set -x
turns on thextrace
shell option (set +x
turns it off) and should work in all Bourne-like shells, likebash
,dash
ksh93
,pdksh
andzsh
. This prompts the shell to display the command that gets executed after alias expansions and variable expansions etc. has been performed.The output will be on the standard error stream of the shell (just like the ordinary prompt) so it will not interfere with redirections of standard output, and it will be preceded by a prompt as defined by the
PS4
shell variable (often+␣
by default).Example with a few functions:
With your specific example (with syntax corrected and added quotes):
(I don't use or have
sudo
on my system, so that error is expected.)Note that there is a common utility already called
install
, so naming your function something else (aptin
?) may be needed if you at some point want to use that utility.Note that the trace output is debugging output. It is a representation of what the shell is doing while executing your command. The output that you see on the screen may not be suitable for shell input.
Also note that I was using
bash
above. Other shells may have another default trace prompt (zsh
incorporates the stringzsh
and the current command's history number, for example) or may not "stack" the prompts up likebash
does for nested calls.I'm runningI used to run withset -x
in all my interactive shells by default. It's nice to see what actually got executed... but I've noticed that programmable tab completion etc. may cause unwanted trace output in some shells, and some shells are a bit verbose in their default trace output (e.g.zsh
).