bash-hackers-wiki/bash4/index.html

36 lines
55 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/bash4/ rel=canonical><link rel=prev href=..><link href=../tags/ 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 4 - a rough overview - 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=#bash-4-a-rough-overview 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 4 - a rough overview </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("(prefers-color-scheme: light)"),input=do
<a id=__codelineno-0-2 name=__codelineno-0-2 href=#__codelineno-0-2></a><span class=nb>echo</span><span class=w> </span><span class=si>${</span><span class=p>@:</span><span class=nv>0</span><span class=p>:</span><span class=nv>1</span><span class=si>}</span>
</code></pre></div> <h3 id=globbing>Globbing<a class=headerlink href=#globbing title="Permanent link">&para;</a></h3> <p>There's a new shell option <code>globstar</code>. When enabled, Bash will perform recursive globbing on <code>**</code> this means it matches all directories and files from the current position in the filesystem, rather than only the current level.</p> <p>The new shell option <code>dirspell</code> enables spelling corrections on directory names during globbing.</p> <p>See <a href=../syntax/expansion/globs/ >globs</a></p> <h2 id=associative-arrays>Associative Arrays<a class=headerlink href=#associative-arrays title="Permanent link">&para;</a></h2> <p>Besides the classic method of integer indexed arrays, Bash 4 supports associative arrays.</p> <p>An associative array is an array indexed by an arbitrary string, something like</p> <div class=highlight><pre><span></span><code><a id=__codelineno-1-1 name=__codelineno-1-1 href=#__codelineno-1-1></a><span class=nb>declare</span><span class=w> </span>-A<span class=w> </span>ASSOC
<a id=__codelineno-1-2 name=__codelineno-1-2 href=#__codelineno-1-2></a>
<a id=__codelineno-1-3 name=__codelineno-1-3 href=#__codelineno-1-3></a>ASSOC<span class=o>[</span>First<span class=o>]=</span><span class=s2>&quot;first element&quot;</span>
<a id=__codelineno-1-4 name=__codelineno-1-4 href=#__codelineno-1-4></a>ASSOC<span class=o>[</span>Hello<span class=o>]=</span><span class=s2>&quot;second element&quot;</span>
<a id=__codelineno-1-5 name=__codelineno-1-5 href=#__codelineno-1-5></a>ASSOC<span class=o>[</span>Peter<span class=w> </span>Pan<span class=o>]=</span><span class=s2>&quot;A weird guy&quot;</span>
</code></pre></div> <p>See <a href=../syntax/arrays/ >arrays</a></p> <h2 id=redirection>Redirection<a class=headerlink href=#redirection title="Permanent link">&para;</a></h2> <p>There is a new <code>&amp;&gt;&gt;</code> redirection operator, which appends the standard output and standard error to the named file. This is the same as the good old <code>&gt;&gt;FILE 2&gt;&amp;1</code> notation.</p> <p>The parser now understands <code>|&amp;</code> as a synonym for <code>2&gt;&amp;1 |</code>, which redirects the standard error for a command through a pipe.</p> <p>See <a href=../syntax/redirection/ >redirection</a></p> <h2 id=interesting-new-shell-variables>Interesting new shell variables<a class=headerlink href=#interesting-new-shell-variables title="Permanent link">&para;</a></h2> <table> <thead> <tr> <th>Variable</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><a href=../syntax/shellvars/#BASHPID>BASHPID</a></td> <td>contains the PID of the current shell (this is different than what <code>$$</code> does!)</td> </tr> <tr> <td><a href=../syntax/shellvars/#PROMPT_DIRTRIM>PROMPT_DIRTRIM</a></td> <td>specifies the max. level of unshortened pathname elements in the prompt</td> </tr> <tr> <td><a href=../syntax/shellvars/#FUNCNEST>FUNCNEST</a></td> <td>control the maximum number of shell function recursions</td> </tr> </tbody> </table> <p>See <a href=../syntax/shellvars/ >shellvars</a></p> <h2 id=interesting-new-shell-options>Interesting new Shell Options<a class=headerlink href=#interesting-new-shell-options title="Permanent link">&para;</a></h2> <p>The mentioned shell options are <strong>off by default</strong> unless otherwise mentioned.</p> <table> <thead> <tr> <th>Option</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>checkjobs</code></td> <td>check for and report any running jobs at shell exit</td> </tr> <tr> <td><code>compat*</code></td> <td>set compatiblity modes for older shell versions (influences regular expression matching in <code>[[ ... ]]</code></td> </tr> <tr> <td><code>dirspell</code></td> <td>enables spelling corrections on directory names during globbing</td> </tr> <tr> <td><code>globstar</code></td> <td>enables recursive globbing with <code>**</code></td> </tr> <tr> <td><code>lastpipe</code></td> <td>(4.2) to execute the last command in a pipeline in the current environment</td> </tr> </tbody> </table> <p>See <a href=../internals/shell_options/ >shell_options</a></p> <h2 id=misc>Misc<a class=headerlink href=#misc title="Permanent link">&para;</a></h2> <ul> <li>If a command is not found, the shell attempts to execute a shell function named <code>command_not_found_handle</code>, supplying the command words as the function arguments. This can be used to display userfriendly messages or perform different command searches</li> <li>The behaviour of the <code>set -e</code> (<code>errexit</code>) mode was changed, it now acts more intuitive (and is better documented in the manpage).</li> <li>The output target for the <code>xtrace</code> (<code>set -x</code>/<code>set +x</code>) feature is configurable <strong>since Bash 4.1</strong> (previously, it was fixed to <code>stderr</code>): a variable named <a href=../syntax/shellvars/#BASH_XTRACEFD>BASH_XTRACEFD</a> can be set to the filedescriptor that should get the output</li> <li>Bash 4.1 is able to log the history to syslog (only to be enabled at compile time in <code>config-top.h</code>)</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-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">November 13, 2024</span> </span> <span class=md-source-file__fact> <spa
</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>