bash-hackers-wiki/scripting/bashbehaviour/index.html

203 lines
62 KiB
HTML
Raw Normal View History

<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link href=https://flokoe.github.io/bash-hackers-wiki/scripting/bashbehaviour/ rel=canonical><link href=../../misc/shell_humor/ rel=prev><link href=../bashchanges/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.5.44"><title>Bash's behaviour - The Bash Hackers Wiki</title><link rel=stylesheet href=../../assets/stylesheets/main.0253249f.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.06af60db.min.css><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"><style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#bashs-behaviour class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="The Bash Hackers Wiki" class="md-header__button md-logo" aria-label="The Bash Hackers Wiki" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> The Bash Hackers Wiki </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Bash's behaviour </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(pref
-o posix&#39; while Bash is running will cause Bash to conform more closely
to the POSIX standard by changing the behavior to match that specified
by POSIX in areas where the Bash default differs.
When invoked as `sh&#39;, Bash enters POSIX mode after reading the startup
files.
The following lists what&#39;s changed when Bash is in `POSIX mode&#39;:
1. When a command in the hash table no longer exists, Bash will
re-search `$PATH&#39; to find the new location. This is also
available with `shopt -s checkhash&#39;.
2. The message printed by the job control code and builtins when a job
exits with a non-zero status is `Done(status)&#39;.
3. The message printed by the job control code and builtins when a job
is stopped is `Stopped(SIGNAME)&#39;, where SIGNAME is, for example,
`SIGTSTP&#39;.
4. The `bg&#39; builtin uses the required format to describe each job
placed in the background, which does not include an indication of
whether the job is the current or previous job.
5. Reserved words appearing in a context where reserved words are
recognized do not undergo alias expansion.
6. The POSIX `PS1&#39; and `PS2&#39; expansions of `!&#39; to the history number
and `!!&#39; to `!&#39; are enabled, and parameter expansion is performed
on the values of `PS1&#39; and `PS2&#39; regardless of the setting of the
`promptvars&#39; option.
7. The POSIX startup files are executed (`$ENV&#39;) rather than the
normal Bash files.
8. Tilde expansion is only performed on assignments preceding a
command name, rather than on all assignment statements on the line.
9. The default history file is `~/.sh_history&#39; (this is the default
value of `$HISTFILE&#39;).
10. The output of `kill -l&#39; prints all the signal names on a single
line, separated by spaces, without the `SIG&#39; prefix.
11. The `kill&#39; builtin does not accept signal names with a `SIG&#39;
prefix.
12. Non-interactive shells exit if FILENAME in `.&#39; FILENAME is not
found.
13. Non-interactive shells exit if a syntax error in an arithmetic
expansion results in an invalid expression.
14. Redirection operators do not perform filename expansion on the word
in the redirection unless the shell is interactive.
15. Redirection operators do not perform word splitting on the word in
the redirection.
16. Function names must be valid shell names. That is, they may not
contain characters other than letters, digits, and underscores, and
may not start with a digit. Declaring a function with an invalid
name causes a fatal syntax error in non-interactive shells.
17. POSIX special builtins are found before shell functions during
command lookup.
18. If a POSIX special builtin returns an error status, a
non-interactive shell exits. The fatal errors are those listed in
the POSIX standard, and include things like passing incorrect
options, redirection errors, variable assignment errors for
assignments preceding the command name, etc.
19. If `CDPATH&#39; is set, the `cd&#39; builtin will not implicitly append
the current directory to it. This means that `cd&#39; will fail if no
valid directory name can be constructed from any of the entries in
`$CDPATH&#39;, even if the a directory with the same name as the name
given as an argument to `cd&#39; exists in the current directory.
20. A non-interactive shell exits with an error status if a variable
assignment error occurs when no command name follows the assignment
statements. A variable assignment error occurs, for example, when
trying to assign a value to a readonly variable.
21. A non-interactive shell exits with an error status if the iteration
variable in a `for&#39; statement or the selection variable in a
`select&#39; statement is a readonly variable.
22. Process substitution is not available.
23. Assignment statements preceding POSIX special builtins persist in
the shell environment after the builtin completes.
24. Assignment statements preceding shell function calls persist in the
shell environment after the function returns, as if a POSIX
special builtin command had been executed.
25. The `export&#39; and `readonly&#39; builtin commands display their output
in the format required by POSIX.
26. The `trap&#39; builtin displays signal names without the leading `SIG&#39;.
27. The `trap&#39; builtin doesn&#39;t check the first argument for a possible
signal specification and revert the signal handling to the original
disposition if it is, unless that argument consists solely of
digits and is a valid signal number. If users want to reset the
handler for a given signal to the original disposition, they
should use `-&#39; as the first argument.
28. The `.&#39; and `source&#39; builtins do not search the current directory
for the filename argument if it is not found by searching `PATH&#39;.
29. Subshells spawned to execute command substitutions inherit the
value of the `-e&#39; option from the parent shell. When not in POSIX
mode, Bash clears the `-e&#39; option in such subshells.
30. Alias expansion is always enabled, even in non-interactive shells.
31. When the `alias&#39; builtin displays alias definitions, it does not
display them with a leading `alias &#39; unless the `-p&#39; option is
supplied.
32. When the `set&#39; builtin is invoked without options, it does not
display shell function names and definitions.
33. When the `set&#39; builtin is invoked without options, it displays
variable values without quotes, unless they contain shell
metacharacters, even if the result contains nonprinting characters.
34. When the `cd&#39; builtin is invoked in LOGICAL mode, and the pathname
constructed from `$PWD&#39; and the directory name supplied as an
argument does not refer to an existing directory, `cd&#39; will fail
instead of falling back to PHYSICAL mode.
35. When the `pwd&#39; builtin is supplied the `-P&#39; option, it resets
`$PWD&#39; to a pathname containing no symlinks.
36. The `pwd&#39; builtin verifies that the value it prints is the same as
the current directory, even if it is not asked to check the file
system with the `-P&#39; option.
37. When listing the history, the `fc&#39; builtin does not include an
indication of whether or not a history entry has been modified.
38. The default editor used by `fc&#39; is `ed&#39;.
39. The `type&#39; and `command&#39; builtins will not report a non-executable
file as having been found, though the shell will attempt to
execute such a file if it is the only so-named file found in
`$PATH&#39;.
40. The `vi&#39; editing mode will invoke the `vi&#39; editor directly when
the `v&#39; command is run, instead of checking `$FCEDIT&#39; and
`$EDITOR&#39;.
41. When the `xpg_echo&#39; option is enabled, Bash does not attempt to
interpret any arguments to `echo&#39; as options. Each argument is
displayed, after escape characters are converted.
There is other POSIX behavior that Bash does not implement by default
even when in POSIX mode. Specifically:
1. The `fc&#39; builtin checks `$EDITOR&#39; as a program to edit history
entries if `FCEDIT&#39; is unset, rather than defaulting directly to
`ed&#39;. `fc&#39; uses `ed&#39; if `EDITOR&#39; is unset.
2. As noted above, Bash requires the `xpg_echo&#39; option to be enabled
for the `echo&#39; builtin to be fully conformant.
Bash can be configured to be POSIX-conformant by default, by specifying
the `--enable-strict-posix-default&#39; to `configure&#39; when building.
</code></pre></div> <div class="admonition warning"> <p class=admonition-title>FIXME</p> <p>help me to find out what breaks in POSIX&reg; mode!</p> </div> <p><u><strong>The POSIX&reg; mode can be switched on by:</strong></u></p> <ul> <li>Bash starting as <code>sh</code> (the basename of <code>argv[0]</code> is <code>sh</code>)</li> <li>starting Bash with the commandline option <code>--posix</code></li> <li>on startup, the environment variable <a href=../../syntax/shellvars/#POSIXLY_CORRECT>POSIXLY_CORRECT</a> is set</li> <li>the command <code>set -o posix</code></li> </ul> <p><u><strong>Tests for the POSIX&reg; mode:</strong></u></p> <ul> <li>the variable <a href=../../syntax/shellvars/#SHELLOPTS>SHELLOPTS</a> contains <code>posix</code> in its list</li> </ul> <h3 id=restricted-shell>Restricted shell<a class=headerlink href=#restricted-shell title="Permanent link">&para;</a></h3> <p>In restricted mode, Bash sets up (and runs) a shell environment that's far more controlled and limited than the standard shell mode. It acts like normal Bash with the following restrictions:</p> <ul> <li>the <code>cd</code> command can't be used to change directories</li> <li>the variables <a href=../../syntax/shellvars/#SHELL>SHELL</a>, <a href=../../syntax/shellvars/#PATH>PATH</a>, <a href=../../syntax/shellvars/#ENV>ENV</a> and <a href=../../syntax/shellvars/#BASH_ENV>BASH_ENV</a> can't be set or unset</li> <li>command names that contain a <code>/</code> (slash) can't be called (hence you're limited to <code>PATH</code>)</li> <li>filenames containing a <code>/</code> (slash) can't be specified as argument to the <code>source</code> or <code>.</code> builtin command</li> <li>filenames containing a <code>/</code> (slash) can't be specified as argument to the <code>-p</code> option of the <code>hash</code> builtin command</li> <li>function definitions are not inherited from the environment at shell startup</li> <li>the environment variable <a href=../../syntax/shellvars/#SHELLOPTS>SHELLOPTS</a> is ignored at startup</li> <li>redirecting output using the <code>&gt;</code>, <code>&gt;|</code>, <code>&lt;&gt;</code>, <code>&gt;&amp;</code>, <code>&amp;&gt;</code>, and <code>&gt;&gt;</code> redirection operators isn't allowed</li> <li>the <code>exec</code> builtin command can't replace the shell with another process</li> <li>adding or deleting builtin commands with the <code>-f</code> and <code>-d</code> options to the enable builtin command is forbidden</li> <li>using the <code>enable</code> builtin command to enable disabled shell builtins doesn't work</li> <li>the <code>-p</code> option to the <code>command</code> builtin command doesn't work</li> <li>turning off restricted mode with <code>set +r</code> or <code>set +o restricted</code> is (of course) forbidden</li> </ul> <p>The "-r" restrictions are turned on <strong>after</strong> Bash has read its startup files.</p> <p>When the command that is run is a shell script, then the restrictions are <strong>turned off</strong> for the (sub-)shell that runs that shell script.</p> <p><u><strong>The restricted shell can be switched on by:</strong></u></p> <ul> <li>calling Bash as <code>rbash</code> (the basename of <code>argv[0]</code> is <code>rbash</code>)</li> <li>calling Bash with the <code>-r</code> option</li> <li>calling Bash with the <code>--restricted</code> option</li> </ul> <p><u><strong>Tests for restricted mode:</strong></u></p> <ul> <li>the special parameter <code>$-</code> contains the letter <code>r</code> (lowercase R)</li> <li>the shell option <code>restricted_shell</code> is set and can be checked by the <code>shopt</code> builtin command</li> </ul> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-
</script> <script>
var giscus = document.querySelector("script[src*=giscus]")
/* Set palette on initial load */
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
giscus.setAttribute("data-theme", theme)
}
/* Register event handlers after documented loaded */
document.addEventListener("DOMContentLoaded", function() {
var ref = document.querySelector("[data-md-component=palette]")
ref.addEventListener("change", function() {
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
/* Instruct Giscus to change theme */
var frame = document.querySelector(".giscus-frame")
frame.contentWindow.postMessage(
{ giscus: { setConfig: { theme } } },
"https://giscus.app"
)
}
})
})
</script> </article> </div> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> Made with <a href=https://squidfunk.github.io/mkdocs-material/ target=_blank rel=noopener> Material for MkDocs </a> </div> </div> </div> </footer> </div> <div class=md-dialog data-md-component=dialog> <div class="md-dialog__inner md-typeset"></div> </div> <script id=__config type=application/json>{"base": "../..", "features": ["navigation.instant", "navigation.tracking", "navigation.tabs", "navigation.sections", "navigation.top", "content.action.view", "content.action.edit", "search.suggest", "search.highlight", "content.code.copy"], "search": "../../assets/javascripts/workers/search.6ce7567c.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script> <script src=../../assets/javascripts/bundle.83f73b43.min.js></script> </body> </html>