Bash - Builtin Commands

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

builtin refers to:

  • a builtin command. See list
  • or to the specific builtin command. See builtin (useful when defining a function whose name is the same as a shell builtin)
Advertising

3 - builtin

builtin shell-builtin [arguments]

The builtin command execute the specified shell builtin, passing it arguments, and return its exit status.

This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function.

The cd builtin is commonly redefined this way.

The return status is false if shell-builtin is not a shell builtin command.

4 - List

You can list enabled and disabled command with the builtin command with the enable command

enable -p

Example of output:

enable .
enable :
enable [
....

List:

Advertising

5 - Syntax

Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts – to signify the end of the options.

For example, the :, true, false, and test builtins do not accept options.

     shopt [-pqsu] [-o] [optname ...]
      Toggle the values of variables controlling optional shell behav-
      ior.  With no options, or with the -p option, a list of all set-
      table options is displayed, with an indication of whether or not
      each is set.  The -p option causes output to be displayed	 in  a
      form  that  may be reused as input.  Other options have the fol-
      lowing meanings:
      -s     Enable (set) each optname.
      -u     Disable (unset) each optname.
      -q     Suppresses normal output (quiet mode); the return	status
	     indicates	whether	 the  optname  is  set	or  unset.  If
	     multiple optname arguments are given with -q, the	return
	     status is zero if all optnames are enabled; non-zero oth-
	     erwise.
      -o     Restricts the values of optname to be those  defined  for
	     the -o option to the set builtin.
      If  either  -s or -u is used with no optname arguments, the dis-
      play is limited to those options which are set or unset, respec-
      tively.	Unless otherwise noted, the shopt options are disabled
      (unset) by default.
      The return status when listing options is zero if	 all  optnames
      are  enabled,  non-zero  otherwise.   When  setting or unsetting
      options, the return status is zero unless an optname  is	not  a
      valid shell option.
      The list of shopt options is:
      cdable_vars
	      If  set,	an  argument to the cd builtin command that is
	      not a directory is assumed to be the name of a  variable
	      whose value is the directory to change to.
      cdspell If set, minor errors in the spelling of a directory com-
	      ponent in a cd command will be  corrected.   The	errors
	      checked for are transposed characters, a missing charac-
	      ter, and one character too many.	 If  a	correction  is
	      found,  the corrected file name is printed, and the com-
	      mand proceeds.  This option is only used by  interactive
	      shells.
      checkhash
	      If set, bash checks that a command found in the hash ta-
	      ble exists before trying to execute  it.	 If  a	hashed
	      command  no  longer exists, a normal path search is per-
	      formed.
      checkwinsize
	      If set, bash checks the window size after	 each  command
	      and,  if	necessary,  updates  the  values  of LINES and
	      COLUMNS.
      cmdhist If set, bash attempts to save all lines of  a  multiple-
	      line  command  in	 the  same history entry.  This allows
	      easy re-editing of multi-line commands.
      dotglob If set, bash includes filenames beginning with a ‘.’  in
	      the results of pathname expansion.
      execfail
	      If set, a non-interactive shell will not exit if it can-
	      not execute the file specified as	 an  argument  to  the
	      exec  builtin  command.	An  interactive shell does not
	      exit if exec fails.
      expand_aliases
	      If set, aliases are expanded as  described  above	 under
	      ALIASES.	This option is enabled by default for interac-
	      tive shells.
      extdebug
	      If set,  behavior	 intended  for	use  by	 debuggers  is
	      enabled:
	      1.     The -F option to the declare builtin displays the
		     source file name and line number corresponding to
		     each function name supplied as an argument.
	      2.     If	 the  command  run by the DEBUG trap returns a
		     non-zero value, the next command is  skipped  and
		     not executed.
	      3.     If	 the  command  run by the DEBUG trap returns a
		     value of 2, and the shell is executing in a  sub-
		     routine  (a shell function or a shell script exe-
		     cuted by the . or source  builtins),  a  call  to
		     return is simulated.
	      4.     BASH_ARGC	and BASH_ARGV are updated as described
		     in their descriptions above.
	      5.     Function tracing is enabled:   command  substitu-
		     tion, shell functions, and subshells invoked with
		     ( command ) inherit the DEBUG and RETURN traps.
	      6.     Error tracing is enabled:	command	 substitution,
		     shell  functions,	and  subshells	invoked with (
		     command ) inherit the ERROR trap.
      extglob If set, the extended pattern matching features described
	      above under Pathname Expansion are enabled.
      extquote
	      If  set,	$'string'  and	$"string" quoting is performed
	      within  ${parameter}  expansions	enclosed   in	double
	      quotes.  This option is enabled by default.
      failglob
	      If  set,	patterns  which fail to match filenames during
	      pathname expansion result in an expansion error.
      force_fignore
	      If set, the suffixes  specified  by  the	FIGNORE	 shell
	      variable	cause words to be ignored when performing word
	      completion even if the ignored words are the only possi-
	      ble  completions.	  See  SHELL  VARIABLES	 above	for  a
	      description of  FIGNORE.	 This  option  is  enabled  by
	      default.
      gnu_errfmt
	      If set, shell error messages are written in the standard
	      GNU error message format.
      histappend
	      If set, the history list is appended to the  file	 named
	      by  the  value  of  the HISTFILE variable when the shell
	      exits, rather than overwriting the file.
      histreedit
	      If set, and readline is being used, a user is given  the
	      opportunity to re-edit a failed history substitution.
      histverify
	      If  set, and readline is being used, the results of his-
	      tory substitution are  not  immediately  passed  to  the
	      shell  parser.   Instead,	 the  resulting line is loaded
	      into the readline editing buffer, allowing further modi-
	      fication.
      hostcomplete
	      If set, and readline is being used, bash will attempt to
	      perform hostname completion when a word containing  a  @
	      is   being  completed  (see  Completing  under  READLINE
	      above).  This is enabled by default.
      huponexit
	      If set, bash will send SIGHUP to all jobs when an inter-
	      active login shell exits.
      interactive_comments
	      If set, allow a word beginning with # to cause that word
	      and all remaining characters on that line to be  ignored
	      in  an  interactive  shell  (see	COMMENTS above).  This
	      option is enabled by default.
      lithist If set, and the cmdhist option  is  enabled,  multi-line
	      commands are saved to the history with embedded newlines
	      rather than using semicolon separators where possible.
      login_shell
	      The shell sets this option if it is started as  a	 login
	      shell  (see  INVOCATION  above).	 The  value may not be
	      changed.
      mailwarn
	      If set, and a file that bash is checking	for  mail  has
	      been  accessed  since  the last time it was checked, the
	      message ‘‘The mail in mailfile has been read’’  is  dis-
	      played.
      no_empty_cmd_completion
	      If  set,	and  readline  is  being  used,	 bash will not
	      attempt to search the PATH for possible completions when
	      completion is attempted on an empty line.
      nocaseglob
	      If  set,	bash  matches  filenames in a case-insensitive
	      fashion when performing pathname expansion (see Pathname
	      Expansion above).
      nocasematch
	      If  set,	bash  matches  patterns	 in a case-insensitive
	      fashion when performing matching while executing case or
	      [[ conditional commands.
      nullglob
	      If  set,	bash allows patterns which match no files (see
	      Pathname Expansion above) to expand to  a	 null  string,
	      rather than themselves.
      progcomp
	      If set, the programmable completion facilities (see Pro-
	      grammable Completion above) are enabled.	This option is
	      enabled by default.
      promptvars
	      If set, prompt strings undergo parameter expansion, com-
	      mand  substitution,  arithmetic  expansion,  and	 quote
	      removal  after  being expanded as described in PROMPTING
	      above.  This option is enabled by default.
      restricted_shell
	      The  shell  sets	this  option  if  it  is  started   in
	      restricted mode (see RESTRICTED SHELL below).  The value
	      may not be changed.  This is not reset when the  startup
	      files  are  executed, allowing the startup files to dis-
	      cover whether or not a shell is restricted.
      shift_verbose
	      If set, the shift builtin prints an error	 message  when
	      the shift count exceeds the number of positional parame-
	      ters.
      sourcepath
	      If set, the source (.) builtin uses the value of PATH to
	      find  the	 directory  containing the file supplied as an
	      argument.	 This option is enabled by default.
      xpg_echo
	      If  set,	the  echo  builtin  expands   backslash-escape
	      sequences by default.
     suspend [-f]
      Suspend  the execution of this shell until it receives a SIGCONT
      signal.  When the suspended shell is a  background  process,  it
      can  be  restarted by the fg command. For more information, read
      the JOB CONTROL section. The suspend command can not suspend the
      login shell.  However, when -f option is specified, suspend com-
      mand can suspend even login  shell.   The	 return	 status	 is  0
      unless  the shell is a login shell and -f is not supplied, or if
      job control is not enabled.
     test expr
     [ expr ]
      Return a status of 0 or 1 depending on  the  evaluation  of  the
      conditional  expression expr.  Each operator and operand must be
      a separate argument.  Expressions are composed of the  primaries
      described	 above	under  CONDITIONAL EXPRESSIONS.	 test does not
      accept any options, nor does it accept and ignore an argument of
      -- as signifying the end of options.
      Expressions  may	be  combined  using  the  following operators,
      listed in decreasing order of precedence.
      ! expr True if expr is false.
      ( expr )
	     Returns the value of expr.	 This may be used to  override
	     the normal precedence of operators.
      expr1 -a expr2
	     True if both expr1 and expr2 are true.
      expr1 -o expr2
	     True if either expr1 or expr2 is true.
      test and [ evaluate conditional expressions using a set of rules
      based on the number of arguments.
      0 arguments
	     The expression is false.
      1 argument
	     The expression is true if and only if the argument is not
	     null.
      2 arguments
	     If the first argument is !, the expression is true if and
	     only if the second argument is null.  If the first	 argu-
	     ment  is  one  of	the unary conditional operators listed
	     above under CONDITIONAL EXPRESSIONS,  the	expression  is
	     true if the unary test is true.  If the first argument is
	     not a valid unary conditional operator, the expression is
	     false.
      3 arguments
	     If	 the  second argument is one of the binary conditional
	     operators listed above under CONDITIONAL EXPRESSIONS, the
	     result of the expression is the result of the binary test
	     using the first and third arguments as operands.  If  the
	     first  argument  is  !,  the value is the negation of the
	     two-argument test using the second and  third  arguments.
	     If the first argument is exactly ( and the third argument
	     is exactly ), the result is the one-argument test of  the
	     second  argument.	 Otherwise,  the  expression is false.
	     The -a and -o operators are considered  binary  operators
	     in this case.
      4 arguments
	     If the first argument is !, the result is the negation of
	     the three-argument expression composed of	the  remaining
	     arguments.	 Otherwise, the expression is parsed and eval-
	     uated according to	 precedence  using  the	 rules	listed
	     above.
      5 or more arguments
	     The  expression  is  parsed  and  evaluated  according to
	     precedence using the rules listed above.
     times  Print the accumulated user and system times for  the  shell  and
      for processes run from the shell.	 The return status is 0.
     trap [-lp] [[arg] sigspec ...]
      The  command  arg	 is  to	 be  read  and executed when the shell
      receives signal(s) sigspec.  If arg is absent (and  there	 is  a
      single  sigspec)	or  -,	each  specified signal is reset to its
      original disposition (the value it  had  upon  entrance  to  the
      shell).	If arg is the null string the signal specified by each
      sigspec is ignored by the shell and by the commands it  invokes.
      If  arg  is  not present and -p has been supplied, then the trap
      commands associated with each  sigspec  are  displayed.	If  no
      arguments	 are  supplied or if only -p is given, trap prints the
      list of commands associated with each  signal.   The  -l	option
      causes  the shell to print a list of signal names and their cor-
      responding numbers.   Each  sigspec  is  either  a  signal  name
      defined  in  <signal.h>,	or  a signal number.  Signal names are
      case insensitive and the SIG prefix is optional.	If  a  sigspec
      is  EXIT (0) the command arg is executed on exit from the shell.
      If a sigspec is DEBUG, the command arg is executed before	 every
      simple command, for command, case command, select command, every
      arithmetic for command, and before the first command executes in
      a	 shell	function  (see	SHELL  GRAMMAR	above).	  Refer to the
      description of the extdebug option  to  the  shopt  builtin  for
      details  of  its effect on the DEBUG trap.  If a sigspec is ERR,
      the command arg is executed whenever  a  simple  command	has  a
      non-zero	exit status, subject to the following conditions.  The
      ERR trap is not executed if the failed command is	 part  of  the
      command  list  immediately  following  a while or until keyword,
      part of the test in an if statement, part of a && or ││ list, or
      if  the  command’s  return value is being inverted via !.	 These
      are the same conditions obeyed by	 the  errexit  option.	 If  a
      sigspec is RETURN, the command arg is executed each time a shell
      function or a script executed with the . or source builtins fin-
      ishes executing.	Signals ignored upon entry to the shell cannot
      be trapped or reset.  Trapped signals that are not being ignored
      are reset to their original values in a child process when it is
      created.	The return status is false if any sigspec is  invalid;
      otherwise trap returns true.
     type [-aftpP] name [name ...]
      With  no options, indicate how each name would be interpreted if
      used as a command name.  If the -t option is used, type prints a
      string  which  is	 one  of alias, keyword, function, builtin, or
      file if  name  is	 an  alias,  shell  reserved  word,  function,
      builtin,	or disk file, respectively.  If the name is not found,
      then nothing  is	printed,  and  an  exit	 status	 of  false  is
      returned.	  If  the  -p  option is used, type either returns the
      name of the disk file that would be executed if name were speci-
      fied as a command name, or nothing if ‘‘type -t name’’ would not
      return file.  The -P option forces a PATH search for each	 name,
      even if ‘‘type -t name’’ would not return file.  If a command is
      hashed, -p and -P print the hashed value,	 not  necessarily  the
      file that appears first in PATH.	If the -a option is used, type
      prints all of the places that contain an executable named	 name.
      This  includes  aliases  and  functions,	if  and only if the -p
      option is not also used.	The table of hashed  commands  is  not
      consulted	 when  using -a.  The -f option suppresses shell func-
      tion lookup, as with the command builtin.	 type returns true  if
      any of the arguments are found, false if none are found.
     ulimit [-SHacdefilmnpqrstuvx [limit]]
      Provides	control	 over the resources available to the shell and
      to processes started by it, on systems that allow such  control.
      The -H and -S options specify that the hard or soft limit is set
      for the given resource.  A hard limit cannot be  increased  once
      it  is set; a soft limit may be increased up to the value of the
      hard limit.  If neither -H nor -S is specified,  both  the  soft
      and  hard limits are set.	 The value of limit can be a number in
      the unit specified for the resource or one of the special values
      hard,  soft,  or	unlimited,  which  stand  for the current hard
      limit, the current soft limit, and no limit,  respectively.   If
      limit  is	 omitted,  the	current value of the soft limit of the
      resource is printed, unless the -H option is given.   When  more
      than  one	 resource  is  specified,  the limit name and unit are
      printed before the value.	 Other options are interpreted as fol-
      lows:
      -a     All current limits are reported
      -c     The maximum size of core files created
      -d     The maximum size of a process’s data segment
      -e     The maximum scheduling priority ("nice")
      -f     The  maximum  size	 of files written by the shell and its
	     children
      -i     The maximum number of pending signals
      -l     The maximum size that may be locked into memory
      -m     The maximum resident set size (has no effect on Linux)
      -n     The maximum number of open file descriptors (most systems
	     do not allow this value to be set)
      -p     The pipe size in 512-byte blocks (this may not be set)
      -q     The maximum number of bytes in POSIX message queues
      -r     The maximum real-time scheduling priority
      -s     The maximum stack size
      -t     The maximum amount of cpu time in seconds
      -u     The  maximum  number  of  processes available to a single
	     user
      -v     The maximum amount of virtual  memory  available  to  the
	     shell
      -x     The maximum number of file locks
      If limit is given, it is the new value of the specified resource
      (the -a option is display only).	If no option is given, then -f
      is  assumed.  Values are in 1024-byte increments, except for -t,
      which is in seconds, -p, which is in units of  512-byte  blocks,
      and  -n and -u, which are unscaled values.  The return status is
      0 unless an invalid option or argument is supplied, or an	 error
      occurs while setting a new limit.
     umask [-p] [-S] [mode]
      The user file-creation mask is set to mode.  If mode begins with
      a digit, it is interpreted as an octal number; otherwise	it  is
      interpreted  as a symbolic mode mask similar to that accepted by
      chmod(1).	 If mode is omitted, the current value of the mask  is
      printed.	 The  -S  option causes the mask to be printed in sym-
      bolic form; the default output is an octal number.   If  the  -p
      option is supplied, and mode is omitted, the output is in a form
      that may be reused as input.  The return status is 0 if the mode
      was  successfully	 changed  or if no mode argument was supplied,
      and false otherwise.
     unalias [-a] [name ...]
      Remove each name from the list of defined	 aliases.   If	-a  is
      supplied,	 all  alias definitions are removed.  The return value
      is true unless a supplied name is not a defined alias.
     unset [-fv] [name ...]
      For each name, remove the corresponding  variable	 or  function.
      If no options are supplied, or the -v option is given, each name
      refers to a shell variable.   Read-only  variables  may  not  be
      unset.   If  -f  is specified, each name refers to a shell func-
      tion, and the function definition is removed.  Each unset	 vari-
      able  or function is removed from the environment passed to sub-
      sequent commands.	 If any of RANDOM, SECONDS,  LINENO,  HISTCMD,
      FUNCNAME, GROUPS, or DIRSTACK are unset, they lose their special
      properties, even if they are subsequently reset.	The exit  sta-
      tus is true unless a name is readonly.
     wait [n ...]
      Wait  for each specified process and return its termination sta-
      tus.  Each n may be a process ID or a job	 specification;	 if  a
      job  spec	 is  given,  all  processes in that job’s pipeline are
      waited for.  If n is not given, all currently active child  pro-
      cesses  are  waited  for,	 and  the return status is zero.  If n
      specifies a non-existent process or job, the  return  status  is
      127.   Otherwise,	 the  return  status is the exit status of the
      last process or job waited for.
Advertising

6 - Documentation / Reference