wiki.bash-hackers.org/scripting/style
2023-04-14 23:44:41 +12:00

648 lines
38 KiB
Plaintext

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr" class="no-js">
<head><script type="text/javascript" src="/_static/js/bundle-playback.js?v=TPXmWR5s" charset="utf-8"></script>
<script type="text/javascript" src="/_static/js/wombat.js?v=txqj7nKC" charset="utf-8"></script>
<script type="text/javascript">
__wm.init("http://web.archive.org/web");
__wm.wombat("https://wiki.bash-hackers.org/scripting/style?do=edit","20220615024158","http://web.archive.org/","web","/_static/",
"1655260918");
</script>
<link rel="stylesheet" type="text/css" href="/_static/css/banner-styles.css?v=S1zqJCYt" />
<link rel="stylesheet" type="text/css" href="/_static/css/iconochive.css?v=qtvMKcIJ" />
<!-- End Wayback Rewrite JS Include -->
<meta charset="UTF-8"/>
<title>Scripting with style [Bash Hackers Wiki]</title>
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<link rel="shortcut icon" href="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/favicon.ico"/>
<link rel="apple-touch-icon" href="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/apple-touch-icon.png"/>
<meta name="generator" content="DokuWiki"/>
<meta name="robots" content="noindex,nofollow"/>
<link type="text/css" rel="stylesheet" href="/web/20220615024158cs_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/bootstrap/default/bootstrap.min.css"/>
<link rel="search" type="application/opensearchdescription+xml" href="/web/20220615024158/https://wiki.bash-hackers.org/lib/exe/opensearch.php" title="Bash Hackers Wiki"/>
<link rel="start" href="/"/>
<link rel="contents" href="/scripting/style?do=index" title="Sitemap"/>
<link rel="manifest" href="/web/20220615024158/https://wiki.bash-hackers.org/lib/exe/manifest.php"/>
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/web/20220615024158/https://wiki.bash-hackers.org/feed.php"/>
<link rel="alternate" type="application/rss+xml" title="Current namespace" href="/web/20220615024158/https://wiki.bash-hackers.org/feed.php?mode=list&amp;ns=scripting"/>
<link rel="alternate" type="text/html" title="Plain HTML" href="/web/20220615024158/https://wiki.bash-hackers.org/_export/xhtml/scripting/style"/>
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/web/20220615024158/https://wiki.bash-hackers.org/_export/raw/scripting/style"/>
<link rel="stylesheet" type="text/css" href="/web/20220615024158cs_/https://wiki.bash-hackers.org/lib/exe/css.php?t=bootstrap3&amp;tseed=54923c3deda180f2db5bd755cd8fbf1a"/>
<!--[if gte IE 9]><!-->
<script type="text/javascript">/*<![CDATA[*/var NS='scripting';var JSINFO = {"updatable":1,"userreplace":1,"default_macro_string":"","plugins":{"edittable":{"default columnwidth":""}},"move_renameokay":false,"isadmin":0,"isauth":0,"bootstrap3":{"mode":"source","toc":[],"config":{"collapsibleSections":0,"fixedTopNavbar":1,"showSemanticPopup":0,"sidebarOnNavbar":0,"tagsOnTop":1,"tocAffix":1,"tocCollapseOnScroll":1,"tocCollapsed":0,"tocLayout":"default","useAnchorJS":1,"useAlternativeToolbarIcons":1}},"id":"scripting:style","namespace":"scripting","ACT":"source","useHeadingNavigation":1,"useHeadingContent":1};
/*!]]>*/</script>
<script type="text/javascript" charset="utf-8" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/exe/jquery.php?tseed=23f888679b4f1dc26eef34902aca964f"></script>
<script type="text/javascript" charset="utf-8" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/exe/js.php?t=bootstrap3&amp;tseed=54923c3deda180f2db5bd755cd8fbf1a"></script>
<script type="text/javascript">/*<![CDATA[*/if (typeof IconifyConfig == 'undefined') { var IconifyConfig = { 'defaultAPI' : '/lib/tpl/bootstrap3/iconify.php?prefix={prefix}&icons={icons}' } }
/*!]]>*/</script>
<script type="text/javascript" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/anchorjs/anchor.min.js"></script>
<script type="text/javascript" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/typeahead/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/iconify/iconify.min.js"></script>
<script type="text/javascript" src="/web/20220615024158js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/iconify/plugins/fa.js"></script>
<!--<![endif]-->
<style type="text/css">@media screen { body { margin-top: 65px; } #dw__toc.affix { top: 55px; position: fixed !important; } #dw__toc .nav .nav .nav { display: none; } }</style>
<!--[if lt IE 9]>
<script type="text/javascript" src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script type="text/javascript" src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body class="default dokuwiki mode_source tpl_bootstrap3 dw-page-on-panel dw-fluid-container" data-page-id="scripting:style"><div class="dokuwiki">
<header id="dokuwiki__header" class="dw-container dokuwiki container-fluid mx-5">
<!-- navbar -->
<nav id="dw__navbar" class="navbar navbar-fixed-top navbar-default" role="navigation">
<div class="dw-container container-fluid mx-5">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand d-flex align-items-center" href="/web/20220615024158/https://wiki.bash-hackers.org/start" accesskey="h" title="Bash Hackers Wiki"><img id="dw__logo" class="pull-left h-100 mr-4" alt="Bash Hackers Wiki" src="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/logo.png"/><div class="pull-right"><div id="dw__title">Bash Hackers Wiki</div></div></a>
</div>
<div class="collapse navbar-collapse">
<div class="navbar-right" id="dw__navbar_items">
<!-- navbar-searchform -->
<form action="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style" accept-charset="utf-8" class="navbar-form navbar-left search" id="dw__search" method="get" role="search">
<div class="input-group">
<input id="qsearch" autocomplete="off" type="search" placeholder="Search" value="" accesskey="f" name="q" class="form-control" title="[F]"/>
<div class="input-group-btn">
<button class="btn btn-default" type="submit" title="Search">
<span class="iconify" data-icon="mdi:magnify"></span> </button>
</div>
</div>
<input type="hidden" name="do" value="search"/>
</form>
<!-- /navbar-searchform -->
<!-- tools-menu -->
<ul class="nav navbar-nav dw-action-icon" id="dw__tools">
<li class="dropdown">
<a href="" class="dropdown-toggle" data-target="#" data-toggle="dropdown" title="" role="button" aria-haspopup="true" aria-expanded="false">
<span class="iconify" data-icon="mdi:wrench"></span> <span class="hidden-lg hidden-md hidden-sm">Tools</span> <span class="caret"></span>
</a>
<ul class="dropdown-menu tools" role="menu">
<li class="dropdown-header">
<span class="iconify" data-icon="mdi:account"></span> User Tools </li>
<li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=register" title="Register" rel="nofollow" class="menuitem register"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M15 14c-2.67 0-8 1.33-8 4v2h16v-2c0-2.67-5.33-4-8-4m-9-4V7H4v3H1v2h3v3h2v-3h3v-2m6 2a4 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 4z"/></svg><span>Register</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=login&amp;sectok=" title="Log In" rel="nofollow" class="menuitem login"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M10 17.25V14H3v-4h7V6.75L15.25 12 10 17.25M8 2h9a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2v-4h2v4h9V4H8v4H6V4a2 2 0 0 1 2-2z"/></svg><span>Log In</span></a></li>
<li class="divider" role="separator"></li>
<li class="dropdown-header">
<span class="iconify" data-icon="mdi:toolbox"></span> Site Tools </li>
<li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=recent" title="Recent Changes [r]" rel="nofollow" accesskey="r" class="menuitem recent"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M15 13h1.5v2.82l2.44 1.41-.75 1.3L15 16.69V13m4-5H5v11h4.67c-.43-.91-.67-1.93-.67-3a7 7 0 0 1 7-7c1.07 0 2.09.24 3 .67V8M5 21a2 2 0 0 1-2-2V5c0-1.11.89-2 2-2h1V1h2v2h8V1h2v2h1a2 2 0 0 1 2 2v6.1c1.24 1.26 2 2.99 2 4.9a7 7 0 0 1-7 7c-1.91 0-3.64-.76-4.9-2H5m11-9.85A4.85 4.85 0 0 0 11.15 16c0 2.68 2.17 4.85 4.85 4.85A4.85 4.85 0 0 0 20.85 16c0-2.68-2.17-4.85-4.85-4.85z"/></svg><span>Recent Changes</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=media&amp;ns=scripting" title="Media Manager" rel="nofollow" class="menuitem media"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M7 15l4.5-6 3.5 4.5 2.5-3L21 15m1-11h-8l-2-2H6a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2M2 6H0v14a2 2 0 0 0 2 2h18v-2H2V6z"/></svg><span>Media Manager</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=index" title="Sitemap [x]" rel="nofollow" accesskey="x" class="menuitem index"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M3 3h6v4H3V3m12 7h6v4h-6v-4m0 7h6v4h-6v-4m-2-4H7v5h6v2H5V9h2v2h6v2z"/></svg><span>Sitemap</span></a></li>
<li class="divider" role="separator"></li>
<li class="dropdown-header">
<span class="iconify" data-icon="mdi:file-document-outline"></span> Page Tools </li>
<li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=" title="Show page [v]" rel="nofollow" accesskey="v" class="menuitem show"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M13 9h5.5L13 3.5V9M6 2h8l6 6v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V4c0-1.11.89-2 2-2m9 16v-2H6v2h9m3-4v-2H6v2h12z"/></svg><span>Show page</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=revisions" title="Old revisions [o]" rel="nofollow" accesskey="o" class="menuitem revs"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M11 7v5.11l4.71 2.79.79-1.28-4-2.37V7m0-5C8.97 2 5.91 3.92 4.27 6.77L2 4.5V11h6.5L5.75 8.25C6.96 5.73 9.5 4 12.5 4a7.5 7.5 0 0 1 7.5 7.5 7.5 7.5 0 0 1-7.5 7.5c-3.27 0-6.03-2.09-7.06-5h-2.1c1.1 4.03 4.77 7 9.16 7 5.24 0 9.5-4.25 9.5-9.5A9.5 9.5 0 0 0 12.5 2z"/></svg><span>Old revisions</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=backlink" title="Backlinks" rel="nofollow" class="menuitem backlink"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M10.59 13.41c.41.39.41 1.03 0 1.42-.39.39-1.03.39-1.42 0a5.003 5.003 0 0 1 0-7.07l3.54-3.54a5.003 5.003 0 0 1 7.07 0 5.003 5.003 0 0 1 0 7.07l-1.49 1.49c.01-.82-.12-1.64-.4-2.42l.47-.48a2.982 2.982 0 0 0 0-4.24 2.982 2.982 0 0 0-4.24 0l-3.53 3.53a2.982 2.982 0 0 0 0 4.24m2.82-4.24c.39-.39 1.03-.39 1.42 0a5.003 5.003 0 0 1 0 7.07l-3.54 3.54a5.003 5.003 0 0 1-7.07 0 5.003 5.003 0 0 1 0-7.07l1.49-1.49c-.01.82.12 1.64.4 2.43l-.47.47a2.982 2.982 0 0 0 0 4.24 2.982 2.982 0 0 0 4.24 0l3.53-3.53a2.982 2.982 0 0 0 0-4.24.973.973 0 0 1 0-1.42z"/></svg><span>Backlinks</span></a></li><li class="action"><a href="#dokuwiki__top" title="Back to top [t]" rel="nofollow" accesskey="t" class="menuitem top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" 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 8v12z"/></svg><span>Back to top</span></a></li>
</ul>
</li>
</ul>
<!-- /tools-menu -->
<ul class="nav navbar-nav">
<li>
<span class="dw__actions dw-action-icon">
<a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=register" title="Register" rel="nofollow" class="menuitem register btn btn-success navbar-btn"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M15 14c-2.67 0-8 1.33-8 4v2h16v-2c0-2.67-5.33-4-8-4m-9-4V7H4v3H1v2h3v3h2v-3h3v-2m6 2a4 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 4z"/></svg><span class=""> Register</span></a><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=login&amp;sectok=" title="Log In" rel="nofollow" class="menuitem login btn btn-default navbar-btn"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M10 17.25V14H3v-4h7V6.75L15.25 12 10 17.25M8 2h9a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2v-4h2v4h9V4H8v4H6V4a2 2 0 0 1 2-2z"/></svg><span class=""> Log In</span></a> </span>
</li>
</ul>
</div>
</div>
</div>
</nav>
<!-- navbar -->
<div align="center">
<script async src="//web.archive.org/web/20220615024158js_/https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- BHORG_BOTTOM -->
<ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-4658830517838678" data-ad-slot="1603598940"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</header>
<a name="dokuwiki__top" id="dokuwiki__top"></a>
<main role="main" class="dw-container pb-5 dokuwiki container-fluid mx-5">
<div id="dokuwiki__pageheader">
<!-- breadcrumbs -->
<nav id="dw__breadcrumbs" class="small">
<hr/>
<div class="dw__youarehere">
<ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li>You are here</li><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="/web/20220615024158/https://wiki.bash-hackers.org/start" itemprop="item" title="start"><span itemprop="name"><span class="iconify" data-icon="mdi:home"></span></span></a></li><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/start" class="wikilink2" title="scripting:start" rel="nofollow">scripting</a></li><li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name"><a itemprop="item" href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style" class="wikilink1" title="scripting:style">Scripting with style</a></span></li></ol> </div>
<div class="dw__breadcrumbs hidden-print">
<ol class="breadcrumb"><li>Trace</li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/commands/builtin/caller" title="commands:builtin:caller">The caller builtin command</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/commands/builtin/export" title="commands:builtin:export">The export builtin command</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/commands/builtin/exit" title="commands:builtin:exit">The exit builtin command</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/dict/terms/exit_status" title="dict:terms:exit_status">Exit Status</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/howto/calculate-dc" title="howto:calculate-dc">Calculating with dc</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/howto/getopts_tutorial" title="howto:getopts_tutorial">Small getopts tutorial</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/internals/shell_options" title="internals:shell_options">List of shell options</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/syntax/shellvars" title="syntax:shellvars">Special parameters and shell variables</a></li><li><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/basics" title="scripting:basics">The basics of shell scripting</a></li><li class="active"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style" title="scripting:style">Scripting with style</a></li></ol> </div>
<hr/>
</nav>
<!-- /breadcrumbs -->
<p class="text-right">
<span class="pageId ml-1 label label-primary">scripting:style</span> </p>
<div id="dw__msgarea" class="small">
</div>
</div>
<div class="row">
<article id="dokuwiki__content" class="col-sm-12 col-md-12 " itemscope itemtype="http://schema.org/Article" itemref="dw__license">
<!-- page-tools -->
<nav id="dw__pagetools" class="hidden-print">
<div class="tools panel panel-default">
<ul class="nav nav-stacked nav-pills text-muted">
<li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=" title="Show page [v]" rel="nofollow" accesskey="v" class="menuitem show"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M13 9h5.5L13 3.5V9M6 2h8l6 6v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V4c0-1.11.89-2 2-2m9 16v-2H6v2h9m3-4v-2H6v2h12z"/></svg><span>Show page</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=revisions" title="Old revisions [o]" rel="nofollow" accesskey="o" class="menuitem revs"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M11 7v5.11l4.71 2.79.79-1.28-4-2.37V7m0-5C8.97 2 5.91 3.92 4.27 6.77L2 4.5V11h6.5L5.75 8.25C6.96 5.73 9.5 4 12.5 4a7.5 7.5 0 0 1 7.5 7.5 7.5 7.5 0 0 1-7.5 7.5c-3.27 0-6.03-2.09-7.06-5h-2.1c1.1 4.03 4.77 7 9.16 7 5.24 0 9.5-4.25 9.5-9.5A9.5 9.5 0 0 0 12.5 2z"/></svg><span>Old revisions</span></a></li><li class="action"><a href="/web/20220615024158/https://wiki.bash-hackers.org/scripting/style?do=backlink" title="Backlinks" rel="nofollow" class="menuitem backlink"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"><path d="M10.59 13.41c.41.39.41 1.03 0 1.42-.39.39-1.03.39-1.42 0a5.003 5.003 0 0 1 0-7.07l3.54-3.54a5.003 5.003 0 0 1 7.07 0 5.003 5.003 0 0 1 0 7.07l-1.49 1.49c.01-.82-.12-1.64-.4-2.42l.47-.48a2.982 2.982 0 0 0 0-4.24 2.982 2.982 0 0 0-4.24 0l-3.53 3.53a2.982 2.982 0 0 0 0 4.24m2.82-4.24c.39-.39 1.03-.39 1.42 0a5.003 5.003 0 0 1 0 7.07l-3.54 3.54a5.003 5.003 0 0 1-7.07 0 5.003 5.003 0 0 1 0-7.07l1.49-1.49c-.01.82.12 1.64.4 2.43l-.47.47a2.982 2.982 0 0 0 0 4.24 2.982 2.982 0 0 0 4.24 0l3.53-3.53a2.982 2.982 0 0 0 0-4.24.973.973 0 0 1 0-1.42z"/></svg><span>Backlinks</span></a></li><li class="action"><a href="#dokuwiki__top" title="Back to top [t]" rel="nofollow" accesskey="t" class="menuitem top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" 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 8v12z"/></svg><span>Back to top</span></a></li> </ul>
</div>
</nav>
<!-- /page-tools -->
<div class="panel panel-default px-3 py-2" itemprop="articleBody">
<div class="page panel-body">
<div class="dw-content-page "><!-- content --><div class="dw-content"><p>
This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong.
</p>
<div class="editBox" role="application">
<div class="toolbar group">
<div id="draft__status" class="draft__status"></div>
<div id="tool__bar" class="tool__bar"></div>
</div>
<form id="dw__editform" method="post" action="" accept-charset="utf-8" class=" form-inline"><div class="no">
<input type="hidden" name="sectok" value=""/><input type="hidden" name="id" value="scripting:style"/><input type="hidden" name="rev" value="0"/><input type="hidden" name="date" value="0"/><input type="hidden" name="prefix" value="."/><input type="hidden" name="suffix" value=""/><input type="hidden" name="changecheck" value="84df0c57e368148028df238ce2974f30"/><input type="hidden" name="target" value="section"/><textarea name="wikitext" id="wiki__text" dir="auto" class="edit form-control" cols="80" rows="10" tabindex="1" readonly="readonly">
====== Scripting with style ======
FIXME continue
These are some coding guidelines that helped me to read and understand my own code over the years. They also will help to produce code that will be a bit more robust than &quot;if something breaks, I know how to fix it&quot;.
This is not a bible, of course. But I have seen so much ugly and terrible code (not only in shell) during all the years, that I'm 100% convinced there needs to be //some// code layout and style. No matter which one you use, use it throughout your code (at least don't change it within the same shellscript file); don't change your code layout with your mood.
Some good code layout helps you to read your own code after a while. And of course it helps others to read the code.
===== Indentation guidelines =====
Indentation is nothing that technically influences a script, it's only for us humans.
I'm used to seeing/using indentation of //two space characters// (though many may prefer 4 spaces, see below in the discussion section):
* it's easy and fast to type
* it's not a hard-tab that's displayed differently in different environments
* it's wide enough to give a visual break and small enough to not waste too much space on the line
Speaking of hard-tabs: Avoid them if possible. They only make trouble. I can imagine one case where they're useful: Indenting [[syntax:redirection#here_documents | here-documents]].
==== Breaking up lines ====
Whenever you need to break lines of long code, you should follow one of these two rules:
__**Indention using command width:**__
&lt;code&gt;
activate some_very_long_option \
some_other_option
&lt;/code&gt;
__**Indention using two spaces:**__
&lt;code&gt;
activate some_very_long_option \
some_other_option
&lt;/code&gt;
Personally, with some exceptions, I prefer the first form because it supports the visual impression of &quot;these belong together&quot;.
==== Breaking compound commands ====
[[syntax:ccmd:intro | Compound commands]] form the structures that make a shell script different from a stupid enumeration of commands. Usually they contain a kind of &quot;head&quot; and a &quot;body&quot; that contains command lists. This type of compound command is relatively easy to indent.
I'm used to (not all points apply to all compound commands, just pick the basic idea):
* put the introducing keyword and the initial command list or parameters on one line (&quot;head&quot;)
* put the &quot;body-introducing&quot; keyword on the same line
* the command list of the &quot;body&quot; on separate lines, indented by two spaces
* put the closing keyword on a separated line, indented like the initial introducing keyword
What?! Well, here again:
== Symbolic ==
&lt;code&gt;
HEAD_KEYWORD parameters; BODY_BEGIN
BODY_COMMANDS
BODY_END
&lt;/code&gt;
== if/then/elif/else ==
This construct is a bit special, because it has keywords (''elif'', ''else'') &quot;in the middle&quot;. The visually appealing way is to indent them like this:
&lt;code&gt;
if ...; then
...
elif ...; then
...
else
...
fi
&lt;/code&gt;
== for ==
&lt;code&gt;
for f in /etc/*; do
...
done
&lt;/code&gt;
== while/until ==
&lt;code&gt;
while [[ $answer != [YyNn] ]]; do
...
done
&lt;/code&gt;
== The case construct ==
The ''case'' construct might need a bit more discussion here, since its structure is a bit more complex.
In general, every new &quot;layer&quot; gets a new indentation level:
&lt;code&gt;
case $input in
hello)
echo &quot;You said hello&quot;
;;
bye)
echo &quot;You said bye&quot;
if foo; then
bar
fi
;;
*)
echo &quot;You said something weird...&quot;
;;
esac
&lt;/code&gt;
Some notes:
* if not 100% needed, the optional left parenthesis on the pattern is not used
* the patterns (''hello)'') and the corresponding action terminator ('';;'') are indented at the same level
* the action command lists are indented one more level (and continue to have their own indentation, if needed)
* though optional, the very last action terminator is given
===== Syntax and coding guidelines =====
==== Cryptic constructs ====
Cryptic constructs, we all know them, we all love them. If they are not 100% needed, avoid them, since nobody except you may be able to decipher them.
It's - just like in C - the middle ground between smart, efficient and readable.
If you need to use a cryptic construct, include a comment that explains what your &quot;monster&quot; does.
==== Variable names ====
Since all reserved variables are ''UPPERCASE'', the safest way is to only use ''lowercase'' variable names. This is true for reading user input, loop counting variables, etc., ... (in the example: ''file'')
* prefer ''lowercase'' variables
* if you use ''UPPERCASE'' names, **do not use reserved variable names** (see [[http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08|SUS]] for an incomplete list)
* if you use ''UPPERCASE'' names, prepend the name with a unique prefix (''MY_'' in the example below)
&lt;code&gt;
#!/bin/bash
# the prefix 'MY_'
MY_LOG_DIRECTORY=/var/adm/
for file in &quot;$MY_LOG_DIRECTORY&quot;/*; do
echo &quot;Found Logfile: $file&quot;
done
&lt;/code&gt;
==== Variable initialization ====
As in C, it's always a good idea to initialize your variables, though, the shell will initialize fresh variables itself (better: Unset variables will generally behave like variables containing a null string).
It's no problem to pass an **environment variable** to the script. If you blindly assume that all variables you use for the first time are **empty**, anybody can **inject** content into a variable by passing it via the environment.
The solution is simple and effective: **Initialize them**
&lt;code&gt;
my_input=&quot;&quot;
my_array=()
my_number=0
&lt;/code&gt;
If you do that for every variable you use, then you also have some in-code documentation for them.
==== Parameter expansion ====
Unless you are really sure what you're doing, **quote every parameter expansion**.
There are some cases where this isn't needed from a technical point of view, e.g.
* inside ''&lt;nowiki&gt;[[ ... ]]&lt;/nowiki&gt;'' (other than the RHS of the ''=='', ''!='', and ''=~'' operators)
* the parameter (''WORD'') in ''case $WORD in ....''
* variable assignment: ''VAR=$WORD''
But quoting these is never a mistake. If you quote every parameter expansion, you'll be safe.
If you need to parse a parameter as a list of words, you can't quote, of course, e.g.
&lt;code&gt;
list=&quot;one two three&quot;
# you MUST NOT quote $list here
for word in $list; do
...
done
&lt;/code&gt;
==== Function names ====
Function names should be all ''lowercase'' and meaningful. The function names should be human readable. A function named ''f1'' may be easy and quick to write down, but for debugging and especially for other people, it reveals nothing. Good names help document your code without using extra comments.
**do not use command names for your functions**. e.g. naming a script or function ''test'', will collide with the UNIX ''test'' command.
Unless absolutely necessary, only use alphanumeric characters and the underscore for function names. ''/bin/ls'' is a valid function name in Bash, but is not a good idea.
==== Command substitution ====
As noted in [[syntax:expansion:cmdsubst | the article about command substitution]], you should use the ''$( ... )'' form.
If portability is a concern, use the backquoted form ''` ... `''.
In any case, if other expansions and word splitting are not wanted, you should quote the command substitution!
==== Eval ====
Well, like Greg says: **&quot;If eval is the answer, surely you are asking the wrong question.&quot;**
Avoid it, unless absolutely neccesary:
* ''eval'' can be your neckshot
* there are most likely other ways to achieve what you want
* if possible, re-think the way your script works, if it seems you can't avoid ''eval'' with your current method
* if you really, really, have to use it, then take care, and be sure about what you're doing
===== Basic structure =====
The basic structure of a script simply reads:
&lt;code&gt;
#!SHEBANG
CONFIGURATION_VARIABLES
FUNCTION_DEFINITIONS
MAIN_CODE
&lt;/code&gt;
==== The shebang ====
If possible (I know it's not always possible!), use [[dict:terms:shebang | a shebang]].
Be careful with ''/bin/sh'': The argument that &quot;on Linux ''/bin/sh'' is Bash&quot; **is a lie** (and technically irrelevant)
The shebang serves two purposes for me:
* it specifies the interpreter to be used when the script file is called directly: If you code for Bash, specify ''bash''!
* it documents the desired interpreter (so: use ''bash'' when you write a Bash-script, use ''sh'' when you write a general Bourne/POSIX script, ...)
==== Configuration variables ====
I call variables that are meant to be changed by the user &quot;configuration variables&quot; here.
Make them easy to find (directly at the top of the script), give them meaningful names and maybe a short comment. As noted above, use ''UPPERCASE'' for them only when you're sure about what you're doing. ''lowercase'' will be the safest.
==== Function definitions ====
Unless there are reasons not to, all function definitions should be declared before the main script code runs. This gives a far better overview and ensures that all function names are known before they are used.
Since a function isn't parsed before it is executed, you usually don't have to ensure they're in a specific order.
The portable form of the function definition should be used, without the ''function'' keyword (here using the [[syntax:ccmd:grouping_plain | grouping compound command]]):
&lt;code&gt;
getargs() {
...
}
&lt;/code&gt;
Speaking about the command grouping in function definitions using ''{ ...; }'': If you don't have a good reason to use another compound command directly, you should always use this one.
===== Behaviour and robustness =====
==== Fail early ====
**Fail early**, this sounds bad, but usually is good. Failing early means to error out as early as possible when checks indicate an error or unmet condition. Failing early means to error out **before** your script begins its work in a potentially broken state.
==== Availability of commands ====
If you use external commands that may not be present on the path, or not installed, check for their availability, then tell the user they're missing.
Example:
&lt;code&gt;
my_needed_commands=&quot;sed awk lsof who&quot;
missing_counter=0
for needed_command in $my_needed_commands; do
if ! hash &quot;$needed_command&quot; &gt;/dev/null 2&gt;&amp;1; then
printf &quot;Command not found in PATH: %s\n&quot; &quot;$needed_command&quot; &gt;&amp;2
((missing_counter++))
fi
done
if ((missing_counter &gt; 0)); then
printf &quot;Minimum %d commands are missing in PATH, aborting\n&quot; &quot;$missing_counter&quot; &gt;&amp;2
exit 1
fi
&lt;/code&gt;
==== Exit meaningfully ====
The [[dict:terms:exit_status | exit code]] is your only way to directly communicate with the calling process without any special provisions.
If your script exits, provide a meaningful exit code. That minimally means:
* ''exit 0'' (zero) if everything is okay
* ''exit 1'' - in general non-zero - if there was an error
This, **and only this**, will enable the calling component to check the operation status of your script.
You know:
**&quot;One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.&quot;** //-- Robert Firth//
===== Misc =====
==== Output and appearance ====
* if the script is interactive, if it works for you and if you think this is a nice feature, you can try to [[snipplets:screen_saverestore | save the terminal content and restore it]] after execution
* output clean and understandable screen messages
* if applicable, you can use colors or specific prefixes to tag error and warning messages
* make it easy for the user to identify those messages
* write normal output to ''STDOUT''. write error, warning and diagnostic messages to ''STDERR''
* enables message filtering
* keeps the script from mixing output data with diagnostic, or error messages
* if the script gives syntax help (''-?'' or ''-h'' or ''--help'' arguments), it should go to ''STDOUT''
* if applicable, write error/diagnostic messages to a logfile
* avoids screen clutter
* messages are available for diagnostic use
==== Input ====
* never blindly assume anything. If you want the user to input a number, **check for numeric input, leading zeros**, etc. If you have specific format or content needs, **always validate the input!**
==== Tooling ====
* some of these guidelines, such as indentation, positioning of &quot;body-introducing&quot; keywords, and portable function declarations, can be enforced by [[https://github.com/mvdan/sh|shfmt]]</textarea>
<div id="wiki__editbar" class="editBar">
<div id="size__ctl">
</div>
</div>
</div></form>
</div>
</div><!-- /content --></div>
</div>
</div>
<div class="small text-right">
<span class="docInfo">
<ul class="list-inline"><li><span class="iconify text-muted" data-icon="mdi:file-document-outline"></span> <span title="scripting/style.txt">scripting/style.txt</span></li><li><span class="iconify text-muted" data-icon="mdi:calendar"></span> Last modified: <span title="2018/04/21 09:21">2018/04/21 09:21</span></li><li class="text-muted">by <bdi>pdeyoreo</bdi></li></ul> </span>
</div>
</article>
</div>
</main>
<footer id="dw__footer" class="dw-container py-5 dokuwiki container-fluid">
<hr/>
<div align="center">
<h3><a target="_blank" href="http://web.archive.org/web/20220615024158/http://www.performing-databases.com/">This site is supported by Performing Databases - your experts for database administration</a></h3>
</div>
<hr/>
<div align="center">
<script async src="//web.archive.org/web/20220615024158js_/https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- BHORG_BOTTOM -->
<ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-4658830517838678" data-ad-slot="1603598940"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<!-- footer -->
<div class="dw-container small container-fluid mx-5">
<div class="footer-dw-title">
<div class="media">
<div class="media-left">
<img src="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/logo.png" alt="Bash Hackers Wiki" class="media-object" style="height:32px"/>
</div>
<div class="media-body">
<div class="row">
<div class="col-sm-2">
<h4 class="media-heading">Bash Hackers Wiki</h4>
<p>
</p>
</div>
<div class="col-sm-10">
</div>
</div>
</div>
</div>
</div>
<div class="footer-license row">
<hr/>
<div id="dw__license" class="col-sm-6">
<p>
<a href="http://web.archive.org/web/20220615024158/http://www.gnu.org/licenses/fdl-1.3.html" title="GNU Free Documentation License 1.3" target="" itemscope itemtype="http://schema.org/CreativeWork" itemprop="license" rel="license" class="license"><img src="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/license/gnufdl.png" width="24" height="24" alt="gnufdl"/> </a> </p>
<p class="small">
Except where otherwise noted, content on this wiki is licensed under the following license:<br/><a href="http://web.archive.org/web/20220615024158/http://www.gnu.org/licenses/fdl-1.3.html" title="GNU Free Documentation License 1.3" target="" itemscope itemtype="http://schema.org/CreativeWork" itemprop="license" rel="license" class="license">GNU Free Documentation License 1.3</a> </p>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
<!-- /footer -->
</footer>
<a href="#dokuwiki__top" class="back-to-top hidden-print btn btn-default" title="skip to content" accesskey="t">
<span class="iconify" data-icon="mdi:chevron-up"></span> </a>
<div id="screen__mode"> <span class="visible-xs-block"></span>
<span class="visible-sm-block"></span>
<span class="visible-md-block"></span>
<span class="visible-lg-block"></span>
</div>
<img src="/web/20220615024158im_/https://wiki.bash-hackers.org/lib/exe/indexer.php?id=scripting%3Astyle&amp;1655260920" width="2" height="1" alt=""/>
</div>
</body>
</html>
<!--
FILE ARCHIVED ON 02:41:58 Jun 15, 2022 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 11:35:17 Apr 14, 2023.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
-->
<!--
playback timings (ms):
captures_list: 73.095
exclusion.robots: 0.143
exclusion.robots.policy: 0.133
RedisCDXSource: 0.678
esindex: 0.009
LoadShardBlock: 46.053 (3)
PetaboxLoader3.datanode: 107.05 (4)
load_resource: 361.824
PetaboxLoader3.resolve: 262.782
-->