Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bash (Unix shell)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Debugging === <!-- Beginning of Table --> <!-- Headers --> {| Table | sort | class="wikitable sortable" style="font-size: smaller;" |+ Bash features which can be useful during debugging. |- ! colspan = 3 rowspan = 1 | Feature ! colspan = 1 rowspan = 2 | POSIX 2024 ! colspan = 1 rowspan = 2 | Description ! colspan = 1 rowspan = 2 | Bash ver. |- ! colspan = 1 rowspan = 1 | Grammar type ! colspan = 1 rowspan = 1 | Formal name ! colspan = 1 rowspan = 1 | Syntax |- <!-- Data --> | style="text-align:left" | Parameter Expansions || Indicate Null or Unset | style="text-align:left" | {{code|"${parameter:?[word]}"|bash}} | {{yes}} | style="text-align:left" | "Where the expansion of [word], perhaps an error message or a line number, is written to STDERR and the shell exits with a non-zero exit code." | {{dunno}} |- | style="text-align:left" | Special Parameters || Exit Status | style="text-align:left" | {{code|"$?"|bash}} | {{yes}} | style="text-align:left" | "Expands to the shortest representation of the decimal exit status." | {{dunno}} |- | style="text-align:left" | Special Parameters || PID of Invoked Shell | style="text-align:left" | {{code|"$$"|bash}} | {{yes}} | style="text-align:left" | "Expands to the shortest representation of the decimal process ID of the invoked shell." | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || set :: xtrace | style="text-align:left" | {{code|set -x|bash}} | {{yes}} | style="text-align:left" | The shell's '''primary means of debugging'''. It "writes to standard error a trace for each command after it expands the command and before it executes it." | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || set :: verbose | style="text-align:left" | {{code|set -v|bash}} | {{yes}} | style="text-align:left" | "Writes its input to standard error as it is read." | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || set :: pipefail | style="text-align:left" | {{code|set -o pipefail|bash}} | {{yes}} | style="text-align:left" | "Derive the exit status of a pipeline from the exit statuses of all of the commands in the pipeline, not just the last (rightmost) command." | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || set :: nounset | style="text-align:left" | {{code|set -u|bash}} | {{yes}} | style="text-align:left" | When enabled, will cause the shell to exit with an error message when it encounters an unset variable expansion. Its use has a number of counter-intuitive pitfalls. | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || set :: errexit | style="text-align:left" | {{code|set -e|bash}} | {{yes}} | style="text-align:left" | ErrExit, is a setting that, when enabled, will, under certain very specific conditions, cause the shell to exit without an error message whenever the shell receives a non-zero exit code. Its use is somewhat controversial, to the extent that any somewhat obscure computer program can be considered controversial. Adherents claim that ErrExit provides an assurance of verifiability in situations where shell scripts "must not fail." However, opponents claim that its use is unreliable, deceptively simple, highly counter-intuitive, rife with gotchas and pitfalls, and in essence "security theater." Numerous developers of Bash have strongly discouraged the use of this particular setting. | {{dunno}} |- | style="text-align:left" | Special Built-In Utility || trap :: EXIT | style="text-align:left" | {{code|trap '[arg]' EXIT|bash}} | {{yes}} | style="text-align:left" | "If a [sigspec] (signal specifier) is 0 or EXIT, [arg] is executed when the shell exits." If [arg] contains expansions, then [arg] should be in single quotes. | {{dunno}} |- | style="text-align:left" | Utility || printf | style="text-align:left" | {{code|printf '<%s>\n' "${var}"|bash}} | {{yes}} | style="text-align:left" | A means of reliably printing the contents of a variable. | {{dunno}} |- | style="text-align:left" | Bash Variables || BASHPID | style="text-align:left" | {{code|"${BASHPID}"|bash}} | {{no}} | style="text-align:left" | "Expands to the process ID of the current bash process."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASHPID |publisher=[[GNU Project]] |id=BASHPID}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_ARGC | style="text-align:left" | {{code|"${BASH_ARGC[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable whose values are the number of parameters in each frame of the current bash execution call stack."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fARGC |publisher=[[GNU Project]] |id=BASH_ARGC}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_ARGV | style="text-align:left" | {{code|"${BASH_ARGV[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable containing all of the parameters in the current bash execution call stack."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fARGV |publisher=[[GNU Project]] |id=BASH_ARGV}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_LINENO | style="text-align:left" | {{code|"${BASH_LINENO[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable whose members are the line numbers in source files where each corresponding member of FUNCNAME was invoked."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fLINENO |publisher=[[GNU Project]] |id=BASH_LINENO}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_REMATCH | style="text-align:left" | {{code|"${BASH_REMATCH[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable whose members are assigned by the =~ binary operator to the <nowiki>[[</nowiki> conditional command."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fREMATCH |publisher=[[GNU Project]] |id=BASH_REMATCH}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_SOURCE | style="text-align:left" | {{code|"${BASH_SOURCE}"|bash}} | {{no}} | style="text-align:left" | "An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fSOURCE |publisher=[[GNU Project]] |id=BASH_SOURCE}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || BASH_XTRACEFD | style="text-align:left" | {{code|"${BASH_XTRACEFD}"|bash}} | {{no}} | style="text-align:left" | "If set to an integer corresponding to a valid file descriptor, Bash will write the trace output generated when βset -xβ is enabled to that file descriptor."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-BASH_005fXTRACEFD |publisher=[[GNU Project]] |id=BASH_XTRACEFD}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || EPOCHREALTIME | style="text-align:left" | {{code|"${EPOCHREALTIME}"|bash}} | {{no}} | style="text-align:left" | "Each time this parameter is referenced, it expands to the number of seconds since the Unix Epoch (see time(3)) as a floating point value with micro-second granularity."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-EPOCHREALTIME |publisher=[[GNU Project]] |id=EPOCHREALTIME}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || FUNCNAME | style="text-align:left" | {{code|"${FUNCNAME[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable containing the names of all shell functions currently in the execution call stack."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-FUNCNAME |publisher=[[GNU Project]] |id=FUNCNAME}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || LINENO | style="text-align:left" | {{code|"${LINENO}"|bash}} | {{no}} | style="text-align:left" | "Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-LINENO |publisher=[[GNU Project]] |id=LINENO}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || PIPESTATUS | style="text-align:left" | {{code|"${PIPESTATUS[@]}"|bash}} | {{no}} | style="text-align:left" | "An array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command)."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-PIPESTATUS |publisher=[[GNU Project]] |id=PIPESTATUS}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || PPID | style="text-align:left" | {{code|"${PPID}"|bash}} | {{no}} | style="text-align:left" | "The process ID of the shell's parent."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-PPID |publisher=[[GNU Project]] |id=PPID}}</ref> | {{dunno}} |- | style="text-align:left" | Bash Variables || PS4 | style="text-align:left" | {{code|"${PS4}"|bash}} | {{no}} | style="text-align:left" | "The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace."<ref>{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-PS4 |publisher=[[GNU Project]] |id=PS4}}</ref> | {{dunno}} |- | style="text-align:left" | Shell Builtin || set :: restricted | style="text-align:left" | {{code|set -r|bash}} | {{no}} | style="text-align:left" | Restricted mode is intended to improve the security of an individual shell instance from a malicious human with physical access to a machine. As threat models have changed, it has become less commonly used now than it once was. | {{dunno}} |- | style="text-align:left" | Shell Builtin || shopt :: extdebug | style="text-align:left" | {{code|shopt -s extdebug|bash}} | {{no}} | style="text-align:left" | "Behavior intended for use by debuggers." | {{dunno}} |- | style="text-align:left" | Shell Builtin || trap :: DEBUG | style="text-align:left" | {{code|trap '[arg]' DEBUG|bash}} | {{no}} | style="text-align:left" | "If a sigspec is DEBUG, the command arg is executed before" certain kinds of commands. | {{dunno}} |- | style="text-align:left" | Shell Builtin || trap :: ERR | style="text-align:left" | {{code|trap '[arg]' ERR|bash}} | {{no}} | style="text-align:left" | "If a sigspec is ERR, the command arg is executed whenever..." certain kinds of commands "return a non-zero exit status", subject to similar restrictions as with ErrExit. | {{dunno}} |- | style="text-align:left" | Shell Builtin || trap :: RETURN | style="text-align:left" | {{code|trap '[arg]' RETURN|bash}} | {{no}} | style="text-align:left" | "If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the. or source builtins finishes executing." | {{dunno}} <!-- Format of table entries |- | style="text-align:left" | foo || | style="text-align:left" | {{code|bar|bash}} | style="text-align:center" | | style="text-align:left" | | {{dunno}} --> |} <!-- End of Table --> <!-- Kinds of references --> <!-- [[Solaris (operating system)#Traditional operating system license (1992 to 2004)|Traditional license]] --> <!-- ! colspan = 1 rowspan = 2 | End of support<ref>{{Cite web |title=Lifetime Support Policies, see Oracle and Sun System Software and Operating Systems (PDF) |url=http://www.oracle.com/us/support/lifetime-support/index.html |url-status=live |archive-url=https://web.archive.org/web/20130429180633/http://www.oracle.com/us/support/lifetime-support/index.html |archive-date=April 29, 2013 |access-date=April 18, 2013 |publisher=Oracle Corporation }}</ref> --> * Shell features specified by [[POSIX]]: ** Parameter Expansions:<ref>{{multiref| [https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html GNU Bash Manual, 3.5.3 Shell Parameter Expansion]| [https://tiswww.case.edu/php/chet/bash/bash.html#lbBB bash(1), Parameter Expansion]| [https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_02 POSIX 2024, 2.6.2 Parameter Expansion]}}</ref> ** Special Parameters:<ref name="GNU Bash Manual, 3.4.2 Special Parameters">{{Cite web |title=Special Parameters (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html |publisher=[[GNU Project]]}}</ref><ref name="POSIX 2024, 2.5.2 Special Parameters">{{Cite web |title=Shell Command Language |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_05_02 |website=pubs.opengroup.org}}</ref> ** Special Built-In Utility {{code|set|bash}}:<ref name="the_set">{{multiref| [https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html GNU Bash Manual, 4.3.1 The Set Builtin]|[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_26 POSIX 2024, set]}}</ref><ref name="bash(1), SHELL BUILTIN COMMANDS">{{Cite web |title=BASH(1) Manual Page |url=https://tiswww.case.edu/php/chet/bash/bash.html#lbDB |website=tiswww.case.edu}}</ref> ** Special Built-In Utility {{code|trap [-lp] [arg] [sigspec β¦]|bash}}:<ref name="GNU Bash Manual, 4.1 Bourne Shell Builtins: trap">{{Cite web |title=Bourne Shell Builtins (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-trap |publisher=[[GNU Project]]}}</ref><ref name="bash(1), SHELL BUILTIN COMMANDS" /> ** Utility {{code|printf|bash}}: a means of reliably printing the contents of a variable: * Bash features not specified by POSIX: ** Bash Variables:<ref name="GNU Bash Manual, 5.2 Bash Variables">{{Cite web |title=Bash Variables (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html |publisher=[[GNU Project]]}}</ref><ref name="bash(1): Shell Variables">{{Cite web |title=BASH(1) Manual Page |url=https://tiswww.case.edu/php/chet/bash/bash.html#lbAW |website=tiswww.case.edu}}</ref> ** Shell Builtin {{code|set|bash}}:<ref name="the_set" /><ref name="bash(1), SHELL BUILTIN COMMANDS" /> ** Shell Builtin {{code|shopt|bash}}:<ref name="GNU Bash Manual, 4.3.2 The Shopt Builtin">{{Cite web |title=The Shopt Builtin (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html |publisher=[[GNU Project]]}}</ref><ref name="bash(1), SHELL BUILTIN COMMANDS" /> ** Shell Builtin {{code|trap [-lp] [arg] [sigspec β¦]|bash}}:<ref name="GNU Bash Manual, 4.1 Bourne Shell Builtins: trap" /><ref name="bash(1), SHELL BUILTIN COMMANDS" /> While POSIX does specify certain uses of the {{code|trap|bash}} builtin, the following signal specs are Bash extensions. * Third party debugging utilities: ** ShellCheck: Shell script analysis tool;<ref name="ShellCheck">{{multiref2| [https://shellcheck.net ShellCheck: Shell script analysis tool]|{{GitHub|koalaman/shellcheck|Github: shellcheck|link=hidden}}}}</ref><ref name="shellcheck, ManKier man page" /> ** devscripts-checkbashisms: Check whether a /bin/sh script contains any common bash-specific constructs;<ref name="Package: devscripts: scripts to make the life of a Debian Package maintainer easier">{{Cite web |title=Debian -- Details of package devscripts in sid |url=https://packages.debian.org/sid/devscripts |website=packages.debian.org}}</ref><ref name="checkbashisms, ManKier man page" /> ** kcov: Code coverage tool without special compilation options;<ref name="Kcov - code coverage">[https://simonkagstrom.github.io/kcov Kcov - code coverage]</ref> ** Bashdb: The Bash symbolic debugger.<ref name="Debugging with the BASH debugger">{{Cite web |title=BASH Debugger |url=https://bashdb.sourceforge.net/bashdb.html |website=bashdb.sourceforge.net}}</ref><ref name="Documentation of Project Homepage">{{Cite mailing list |title=[Bashdb-devel] Re: [PATCH] fix bashdb script handling of tmp directory |mailing-list=bug-bash |url=https://lists.gnu.org/archive/html/bug-bash/2005-09/msg00038.html }}</ref> <!-- Other possibly relevant packages: shfmt libatf-sh pcp-pmda-bash pcp-pmda-shping --> ==== Examples ==== With the {{code|:?|bash}} parameter expansion, an unset or null variable can halt a script. * ex.sh *: <syntaxhighlight lang="bash"> #!/bin/bash bar="foo is not defined" echo "${foo:?$bar}" echo this message doesn't print </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./ex.sh ./ex.sh: line 3: foo: foo is not defined </syntaxhighlight> Reliably printing the contents of an array that contains spaces and newlines first in a portable syntax, and then the same thing in Bash. Note that in Bash, the number of spaces before the newline is made clear. : <syntaxhighlight lang="console"> $ # In POSIX shell: $ array=( "a" "b" " > c " ) $ printf ',%s,\n' "${array[@]}" ,a, , b, , c, </syntaxhighlight> : <syntaxhighlight lang="bash"> # In Bash: declare -p array declare -a array=([0]="a" [1]=" b" [2]=$' \n c ') </syntaxhighlight> Printing an error message when there's a problem. * error.sh *: <syntaxhighlight lang="bash"> if ! lsblk | grep sdb then echo Error, line $LINENO fi </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./error.sh Error, line 130 </syntaxhighlight> Using [[xtrace]]. If errexit had been enabled, then {{code|echo quux|bash}} would not have been executed. * test.sh *: <syntaxhighlight lang="bash"> #!/bin/bash set -x foo=bar; echo $foo false echo quux </syntaxhighlight> *: <syntaxhighlight lang="console"> $ ./test.sh + foo=bar + echo bar bar + false + echo quux quux </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)