2

Simple question, really...

Builtin?

What happens when you run: builtin?

The return type from echo $? is 0.

Which means that the command has most likely run successfully.

So, what does running builtin accomplish?

NerdOfCode
  • 2,498

2 Answers2

5

This is useful when you wish to reimplement a shell builtin as a shell function, but need to execute the builtin within the function.

$ type echo
echo is a shell builtin

$ function echo(){ builtin echo "'$1'"; }

$ echo hi
'hi'

help builtin 

builtin: builtin [shell-builtin [arg ...]] Execute shell builtins.

Execute SHELL-BUILTIN with arguments ARGs without performing command
lookup.  This is useful when you wish to reimplement a shell builtin
as a shell function, but need to execute the builtin within the function.

Exit Status:
Returns the exit status of SHELL-BUILTIN, or false if SHELL-BUILTIN is
not a shell builtin.
Ravexina
  • 55,668
  • 25
  • 164
  • 183
2

From help -m builtin:

NAME
    builtin - Execute shell builtins.

SYNOPSIS
    builtin [shell-builtin [arg ...]]

DESCRIPTION
    Execute shell builtins.

    Execute SHELL-BUILTIN with arguments ARGs without performing command
    lookup.  This is useful when you wish to reimplement a shell builtin
    as a shell function, but need to execute the builtin within the function.

Example usage:

cd (){
    builtin cd "$@"
    pwd
}

This cd's, then prints the new working directory (like in IPython). If you forget the builtin part, it will keep calling itself in an infinite loop.

wjandrea
  • 14,236
  • 4
  • 48
  • 98