mirror of
https://github.com/rawiriblundell/wiki.bash-hackers.org
synced 2024-12-24 13:50:39 +01:00
819 lines
58 KiB
Plaintext
819 lines
58 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/syntax/arrays?do=edit","20230325102835","http://web.archive.org/","web","/_static/",
|
|
"1679740115");
|
|
</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>Arrays [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/20230325102835im_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/images/favicon.ico"/>
|
|
<link rel="apple-touch-icon" href="/web/20230325102835im_/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/20230325102835cs_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/bootstrap/default/bootstrap.min.css"/>
|
|
<link rel="search" type="application/opensearchdescription+xml" href="/web/20230325102835/https://wiki.bash-hackers.org/lib/exe/opensearch.php" title="Bash Hackers Wiki"/>
|
|
<link rel="start" href="/"/>
|
|
<link rel="contents" href="/syntax/arrays?do=index" title="Sitemap"/>
|
|
<link rel="manifest" href="/web/20230325102835/https://wiki.bash-hackers.org/lib/exe/manifest.php"/>
|
|
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/web/20230325102835/https://wiki.bash-hackers.org/feed.php"/>
|
|
<link rel="alternate" type="application/rss+xml" title="Current namespace" href="/web/20230325102835/https://wiki.bash-hackers.org/feed.php?mode=list&ns=syntax"/>
|
|
<link rel="alternate" type="text/html" title="Plain HTML" href="/web/20230325102835/https://wiki.bash-hackers.org/_export/xhtml/syntax/arrays"/>
|
|
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/web/20230325102835/https://wiki.bash-hackers.org/_export/raw/syntax/arrays"/>
|
|
<link rel="stylesheet" type="text/css" href="/web/20230325102835cs_/https://wiki.bash-hackers.org/lib/exe/css.php?t=bootstrap3&tseed=54923c3deda180f2db5bd755cd8fbf1a"/>
|
|
<!--[if gte IE 9]><!-->
|
|
<script type="text/javascript">/*<![CDATA[*/var NS='syntax';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":"syntax:arrays","namespace":"syntax","ACT":"source","useHeadingNavigation":1,"useHeadingContent":1};
|
|
/*!]]>*/</script>
|
|
<script type="text/javascript" charset="utf-8" src="/web/20230325102835js_/https://wiki.bash-hackers.org/lib/exe/jquery.php?tseed=23f888679b4f1dc26eef34902aca964f"></script>
|
|
<script type="text/javascript" charset="utf-8" src="/web/20230325102835js_/https://wiki.bash-hackers.org/lib/exe/js.php?t=bootstrap3&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/20230325102835js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/bootstrap/js/bootstrap.min.js"></script>
|
|
<script type="text/javascript" src="/web/20230325102835js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/anchorjs/anchor.min.js"></script>
|
|
<script type="text/javascript" src="/web/20230325102835js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/typeahead/bootstrap3-typeahead.min.js"></script>
|
|
<script type="text/javascript" src="/web/20230325102835js_/https://wiki.bash-hackers.org/lib/tpl/bootstrap3/assets/iconify/iconify.min.js"></script>
|
|
<script type="text/javascript" src="/web/20230325102835js_/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="syntax:arrays"><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/20230325102835/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/20230325102835im_/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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays" 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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?do=login&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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?do=media&ns=syntax" 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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?do=login&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/20230325102835js_/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/20230325102835/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/20230325102835/https://wiki.bash-hackers.org/syntax/start" class="wikilink1" title="syntax:start">Syntax</a></li><li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name"><a itemprop="item" href="/web/20230325102835/https://wiki.bash-hackers.org/syntax/arrays" class="wikilink1" title="syntax:arrays">Arrays</a></span></li></ol> </div>
|
|
|
|
<div class="dw__breadcrumbs hidden-print">
|
|
<ol class="breadcrumb"><li>Trace</li> </div>
|
|
|
|
<hr/>
|
|
|
|
</nav>
|
|
<!-- /breadcrumbs -->
|
|
|
|
<p class="text-right">
|
|
<span class="pageId ml-1 label label-primary">syntax:arrays</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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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/20230325102835/https://wiki.bash-hackers.org/syntax/arrays?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="syntax:arrays"/><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="845fba703e5ab95f18423a1c8ced591c"/><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">
|
|
====== Arrays ======
|
|
|
|
===== Purpose =====
|
|
|
|
An array is a parameter that holds mappings from keys to values. Arrays are used to store a collection of parameters into a parameter. Arrays (in any programming language) are a useful and common composite data structure, and one of the most important scripting features in Bash and other shells.
|
|
|
|
Here is an **abstract** representation of an array named ''NAMES''. The indexes go from 0 to 3.
|
|
<code>
|
|
NAMES
|
|
0: Peter
|
|
1: Anna
|
|
2: Greg
|
|
3: Jan
|
|
</code>
|
|
|
|
Instead of using 4 separate variables, multiple related variables are grouped grouped together into //elements// of the array, accessible by their //key//. If you want the second name, ask for index 1 of the array ''NAMES''.
|
|
|
|
===== Indexing =====
|
|
|
|
Bash supports two different types of ksh-like one-dimensional arrays. **Multidimensional arrays are not implemented**.
|
|
* //Indexed arrays// use positive integer numbers as keys. Indexed arrays are **always sparse**, meaning indexes are not necessarily contiguous. All syntax used for both assigning and dereferencing indexed arrays is an [[syntax:arith_expr | arithmetic evaluation context]] (see [[#Referencing]]). As in C and many other languages, the numerical array indexes start at 0 (zero). Indexed arrays are the most common, useful, and portable type. Indexed arrays were first introduced to Bourne-like shells by ksh88. Similar, partially compatible syntax was inherited by many derivatives including Bash. Indexed arrays always carry the ''-a'' attribute.
|
|
* //Associative arrays// (sometimes known as a "hash" or "dict") use arbitrary nonempty strings as keys. In other words, associative arrays allow you to look up a value from a table based upon its corresponding string label. **Associative arrays are always unordered**, they merely //associate// key-value pairs. If you retrieve multiple values from the array at once, you can't count on them coming out in the same order you put them in. Associative arrays always carry the ''-A'' attribute, and unlike indexed arrays, Bash requires that they always be declared explicitly (as indexed arrays are the default, see [[#Declaration | declaration]]). Associative arrays were first introduced in ksh93, and similar mechanisms were later adopted by Zsh and Bash version 4. These three are currently the only POSIX-compatible shells with any associative array support.
|
|
|
|
===== Syntax =====
|
|
|
|
==== Referencing ====
|
|
|
|
To accommodate referring to array variables and their individual elements, Bash extends the parameter naming scheme with a subscript suffix. Any valid ordinary scalar parameter name is also a valid array name: ''<nowiki>[[:alpha:]_][[:alnum:]_]*</nowiki>''. The parameter name may be followed by an optional subscript enclosed in square brackets to refer to a member of the array.
|
|
|
|
The overall syntax is ''arrname[subscript]'' - where for indexed arrays, ''subscript'' is any valid arithmetic expression, and for associative arrays, any nonempty string. Subscripts are first processed for parameter and arithmetic expansions, and command and process substitutions. When used within parameter expansions or as an argument to the [[commands/builtin/unset | unset]] builtin, the special subscripts ''*'' and ''@'' are also accepted which act upon arrays analogously to the way the ''@'' and ''*'' special parameters act upon the positional parameters. In parsing the subscript, bash ignores any text that follows the closing bracket up to the end of the parameter name.
|
|
|
|
With few exceptions, names of this form may be used anywhere ordinary parameter names are valid, such as within [[syntax:arith_expr | arithmetic expressions]], [[syntax:pe | parameter expansions]], and as arguments to builtins that accept parameter names. An //array// is a Bash parameter that has been given the ''-a'' (for indexed) or ''-A'' (for associative) //attributes//. However, any regular (non-special or positional) parameter may be validly referenced using a subscript, because in most contexts, referring to the zeroth element of an array is synonymous with referring to the array name without a subscript.
|
|
|
|
<code>
|
|
# "x" is an ordinary non-array parameter.
|
|
$ x=hi; printf '%s ' "$x" "${x[0]}"; echo "${_[0]}"
|
|
hi hi hi
|
|
</code>
|
|
|
|
The only exceptions to this rule are in a few cases where the array variable's name refers to the array as a whole. This is the case for the ''unset'' builtin (see [[#Destruction | destruction]]) and when declaring an array without assigning any values (see [[#Declaration | declaration]]).
|
|
|
|
==== Declaration ====
|
|
|
|
The following explicitly give variables array attributes, making them arrays:
|
|
|
|
^Syntax ^Description ^
|
|
|''ARRAY=()'' |Declares an **indexed** array ''ARRAY'' and initializes it to be empty. This can also be used to empty an existing array. |
|
|
|''ARRAY[0]='' |Generally sets the first element of an **indexed** array. If no array ''ARRAY'' existed before, it is created. |
|
|
|''declare -a ARRAY'' |Declares an **indexed** array ''ARRAY''. An existing array is not initialized. |
|
|
|''declare -A ARRAY'' |Declares an **associative** array ''ARRAY''. This is the one and only way to create associative arrays. |
|
|
|
|
As an example, and for use below, let's declare our ''NAMES'' array as described [[#purpose|above]]:
|
|
|
|
declare -a NAMES=('Peter' 'Anna' 'Greg' 'Jan')
|
|
|
|
==== Storing values ====
|
|
|
|
Storing values in arrays is quite as simple as storing values in normal variables.
|
|
|
|
^ Syntax ^ Description ^
|
|
| ''ARRAY[N]=VALUE'' | Sets the element ''N'' of the **indexed** array ''ARRAY'' to ''VALUE''. **''N'' can be any valid [[syntax:arith_expr| arithmetic expression]]**. |
|
|
| ''ARRAY[STRING]=VALUE'' | Sets the element indexed by ''STRING'' of the **associative array** ''ARRAY''. |
|
|
| ''ARRAY=VALUE'' | As above. If no index is given, as a default the zeroth element is set to ''VALUE''. Careful, this is even true of associative arrays - there is no error if no key is specified, and the value is assigned to string index "0". |
|
|
| ''ARRAY=(E1\ E2\ ...)'' | Compound array assignment - sets the whole array ''ARRAY'' to the given list of elements indexed sequentially starting at zero. The array is unset before assignment unless the += operator is used. When the list is empty (''ARRAY=()''), the array will be set to an empty array. This method obviously does not use explicit indexes. An **associative array** can **not** be set like that! Clearing an associative array using ''ARRAY=()'' works. |
|
|
| ''ARRAY=([X]=E1\ [Y]=E2\ ...)'' | Compound assignment for indexed arrays with index-value pairs declared individually (here for example ''X'' and ''Y''). X and Y are arithmetic expressions. This syntax can be combined with the above - elements declared without an explicitly specified index are assigned sequentially starting at either the last element with an explicit index, or zero. |
|
|
| ''ARRAY=([S1]=E1\ [S2]=E2\ ...)'' | Individual mass-setting for **associative arrays**. The named indexes (here: ''S1'' and ''S2'') are strings. |
|
|
| ''ARRAY+=(E1\ E2\ ...)'' | Append to ARRAY. |
|
|
| ''ARRAY=("${ANOTHER_ARRAY[@]}")'' | Copy ANOTHER_ARRAY to ARRAY, copying each element. |
|
|
|
|
As of now, arrays can't be exported.
|
|
==== Getting values ====
|
|
<note>
|
|
For completeness and details on several parameter expansion variants, see the [[syntax:pe|article about parameter expansion]] and check the notes about arrays.
|
|
</note>
|
|
|
|
^Syntax ^Description ^
|
|
| ''${ARRAY[N]}'' | Expands to the value of the index ''N'' in the **indexed** array ''ARRAY''. If ''N'' is a negative number, it's treated as the offset from the maximum assigned index (can't be used for assignment) - 1 |
|
|
| ''${ARRAY[S]}'' | Expands to the value of the index ''S'' in the **associative** array ''ARRAY''. |
|
|
| ''"${ARRAY[@]}"\\ ${ARRAY[@]}\\ "${ARRAY[*]}"\\ ${ARRAY[*]}'' | Similar to [[scripting:posparams#mass_usage| mass-expanding positional parameters]], this expands to all elements. If unquoted, both subscripts ''*'' and ''@'' expand to the same result, if quoted, ''@'' expands to all elements individually quoted, ''*'' expands to all elements quoted as a whole. |
|
|
| ''"${ARRAY[@]:N:M}"\\ ${ARRAY[@]:N:M}\\ "${ARRAY[*]:N:M}"\\ ${ARRAY[*]:N:M}'' | Similar to what this syntax does for the characters of a single string when doing [[syntax:pe#substring_expansion| substring expansion]], this expands to ''M'' elements starting with element ''N''. This way you can mass-expand individual indexes. The rules for quoting and the subscripts ''*'' and ''@'' are the same as above for the other mass-expansions. |
|
|
|
|
For clarification: When you use the subscripts ''@'' or ''*'' for mass-expanding, then the behaviour is exactly what it is for ''$@'' and ''$*'' when [[scripting:posparams#mass_usage | mass-expanding the positional parameters]]. You should read this article to understand what's going on.
|
|
|
|
==== Metadata ====
|
|
|
|
^Syntax ^Description ^
|
|
|''${#ARRAY[N]}'' |Expands to the **length** of an individual array member at index ''N'' (**stringlength**) |
|
|
|''${#ARRAY[STRING]}'' | Expands to the **length** of an individual associative array member at index ''STRING'' (**stringlength**) |
|
|
|''${#ARRAY[@]}''\\ ''${#ARRAY[*]}''|Expands to the **number of elements** in ''ARRAY'' |
|
|
|''${!ARRAY[@]}''\\ ''${!ARRAY[*]}''|Expands to the **indexes** in ''ARRAY'' since BASH 3.0|
|
|
|
|
==== Destruction ====
|
|
The [[commands/builtin/unset | unset]] builtin command is used to destroy (unset) arrays or individual elements of arrays.
|
|
|
|
^Syntax ^Description ^
|
|
|''unset -v ARRAY''\\ ''unset -v ARRAY[@]''\\ ''unset -v ARRAY[*]'' |Destroys a complete array |
|
|
|''unset -v ARRAY[N]''|Destroys the array element at index ''N'' |
|
|
|''unset -v ARRAY[STRING]''|Destroys the array element of the associative array at index ''STRING'' |
|
|
|
|
It is best to [[commands/builtin/unset#portability_considerations | explicitly specify -v]] when unsetting variables with unset.
|
|
|
|
<note warning>
|
|
Specifying unquoted array elements as arguments to any command, such as with the syntax above **may cause [[syntax:expansion:globs | pathname expansion]] to occur** due to the presence of glob characters.
|
|
|
|
Example: You are in a directory with a file named ''x1'', and you want to destroy an array element ''x[1]'', with
|
|
<code>
|
|
unset x[1]
|
|
</code>
|
|
then pathname expansion will expand to the filename ''x1'' and break your processing!
|
|
|
|
Even worse, if ''nullglob'' is set, your array/index will disappear.
|
|
|
|
To avoid this, **always quote** the array name and index:
|
|
<code>
|
|
unset -v 'x[1]'
|
|
</code>
|
|
|
|
This applies generally to all commands which take variable names as arguments. Single quotes preferred.
|
|
</note>
|
|
|
|
===== Usage =====
|
|
|
|
==== Numerical Index ====
|
|
|
|
Numerical indexed arrays are easy to understand and easy to use. The [[#purpose|Purpose]] and [[#indexing|Indexing]] chapters above more or less explain all the needed background theory.
|
|
|
|
Now, some examples and comments for you.
|
|
|
|
Let's say we have an array ''sentence'' which is initialized as follows:
|
|
<code>
|
|
sentence=(Be liberal in what you accept, and conservative in what you send)
|
|
</code>
|
|
|
|
Since no special code is there to prevent word splitting (no quotes), every word there will be assigned to an individual array element. When you count the words you see, you should get 12. Now let's see if Bash has the same opinion:
|
|
|
|
<code>
|
|
$ echo ${#sentence[@]}
|
|
12
|
|
</code>
|
|
|
|
Yes, 12. Fine. You can take this number to walk through the array. Just **subtract 1 from the number of elements, and start your walk at 0 (zero)**:
|
|
<code>
|
|
((n_elements=${#sentence[@]}, max_index=n_elements - 1))
|
|
|
|
for ((i = 0; i <= max_index; i++)); do
|
|
echo "Element $i: '${sentence[i]}'"
|
|
done
|
|
</code>
|
|
|
|
You always have to remember that, it seems newbies have problems sometimes. Please understand that **numerical array indexing begins at 0 (zero)**!
|
|
|
|
The method above, walking through an array by just knowing its number of elements, only works for arrays where all elements are set, of course. If one element in the middle is removed, then the calculation is nonsense, because the number of elements doesn't correspond to the highest used index anymore (we call them "//sparse arrays//").
|
|
|
|
Now, suppose that you want to replace your array ''sentence'' with the values in the [[#purpose|previously-declared array]] ''NAMES'' . You might think you could just do
|
|
|
|
<code>
|
|
$ unset sentence ; declare -a sentence=NAMES
|
|
$ echo ${#sentence[@]}
|
|
1
|
|
# omit calculating max_index as above, and iterate as one-liner
|
|
$ for ((i = 0; i < ${#sentence[@]}; i++)); do echo "Element $i: '${sentence[i]}'" ; done
|
|
Element 0: 'NAMES'
|
|
</code>
|
|
|
|
Obviously that's wrong. What about
|
|
|
|
<code>
|
|
$ unset sentence ; declare -a sentence=${NAMES}
|
|
</code>
|
|
|
|
? Again, wrong:
|
|
|
|
<code>
|
|
$ echo ${#sentence[*]}
|
|
1
|
|
$ for ((i = 0; i < ${#sentence[@]}; i++)); do echo "Element $i: '${sentence[i]}'" ; done
|
|
Element 0: 'Peter'
|
|
</code>
|
|
|
|
So what's the **right** way? The (slightly ugly) answer is, reuse the enumeration syntax:
|
|
|
|
<code>
|
|
$ unset sentence ; declare -a sentence=("${NAMES[@]}")
|
|
$ echo ${#sentence[@]}
|
|
4
|
|
$ for ((i = 0; i < ${#sentence[@]}; i++)); do echo "Element $i: '${sentence[i]}'" ; done
|
|
Element 0: 'Peter'
|
|
Element 1: 'Anna'
|
|
Element 2: 'Greg'
|
|
Element 3: 'Jan'
|
|
</code>
|
|
|
|
==== Associative (Bash 4) ====
|
|
|
|
Associative arrays (or //hash tables//) are not much more complicated than numerical indexed arrays. The numerical index value (in Bash a number starting at zero) just is replaced with an arbitrary string:
|
|
|
|
<code>
|
|
# declare -A, introduced with Bash 4 to declare an associative array
|
|
declare -A sentence
|
|
|
|
sentence[Begin]='Be liberal in what'
|
|
sentence[Middle]='you accept, and conservative'
|
|
sentence[End]='in what you send'
|
|
sentence['Very end']=...
|
|
</code>
|
|
|
|
__**Beware:**__ don't rely on the fact that the elements are ordered in memory like they were declared, it could look like this:
|
|
<code>
|
|
# output from 'set' command
|
|
sentence=([End]="in what you send" [Middle]="you accept, and conservative " [Begin]="Be liberal in what " ["Very end"]="...")
|
|
</code>
|
|
This effectively means, you can get the data back with ''"${sentence[@]}"'', of course (just like with numerical indexing), but you can't rely on a specific order. If you want to store ordered data, or re-order data, go with numerical indexes. For associative arrays, you usually query known index values:
|
|
<code>
|
|
for element in Begin Middle End "Very end"; do
|
|
printf "%s" "${sentence[$element]}"
|
|
done
|
|
printf "\n"
|
|
</code>
|
|
|
|
|
|
**A nice code example:** Checking for duplicate files using an associative array indexed with the SHA sum of the files:
|
|
<code>
|
|
# Thanks to Tramp in #bash for the idea and the code
|
|
|
|
unset flist; declare -A flist;
|
|
while read -r sum fname; do
|
|
if [[ ${flist[$sum]} ]]; then
|
|
printf 'rm -- "%s" # Same as >%s<\n' "$fname" "${flist[$sum]}"
|
|
else
|
|
flist[$sum]="$fname"
|
|
fi
|
|
done < <(find . -type f -exec sha256sum {} +) >rmdups
|
|
</code>
|
|
|
|
==== Integer arrays ====
|
|
|
|
Any type attributes applied to an array apply to all elements of the array. If the integer attribute is set for either indexed or associative arrays, then values are considered as arithmetic for both compound and ordinary assignment, and the += operator is modified in the same way as for ordinary integer variables.
|
|
|
|
<code>
|
|
~ $ ( declare -ia 'a=(2+4 [2]=2+2 [a[2]]="a[2]")' 'a+=(42 [a[4]]+=3)'; declare -p a )
|
|
declare -ai a='([0]="6" [2]="4" [4]="7" [5]="42")'
|
|
</code>
|
|
|
|
''a[0]'' is assigned to the result of ''2+4''. ''a[2]'' gets the result of ''2+2''. The last index in the first assignment is the result of ''a[2]'', which has already been assigned as ''4'', and its value is also given ''a[2]''.
|
|
|
|
This shows that even though any existing arrays named ''a'' in the current scope have already been unset by using ''='' instead of ''+='' to the compound assignment, arithmetic variables within keys can self-reference any elements already assigned within the same compound-assignment. With integer arrays this also applies to expressions to the right of the ''=''. (See [[#evaluation_order | evaluation order]], the right side of an arithmetic assignment is typically evaluated first in Bash.)
|
|
|
|
The second compound assignment argument to declare uses ''+='', so it appends after the last element of the existing array rather than deleting it and creating a new array, so ''a[5]'' gets ''42''.
|
|
|
|
Lastly, the element whose index is the value of ''a[4]'' (''4''), gets ''3'' added to its existing value, making ''a[4]'' == ''7''. Note that having the integer attribute set this time causes += to add, rather than append a string, as it would for a non-integer array.
|
|
|
|
The single quotes force the assignments to be evaluated in the environment of ''declare''. This is important because attributes are only applied to the assignment after assignment arguments are processed. Without them the ''+='' compound assignment would have been invalid, and strings would have been inserted into the integer array without evaluating the arithmetic. A special-case of this is shown in the next section.
|
|
|
|
<note>
|
|
Bash declaration commands are really keywords in disguise. They magically parse arguments to determine whether they are in the form of a valid assignment. If so, they are evaluated as assignments. If not, they are undergo normal argument expansion before being passed to the builtin which evaluates the resulting string as an assignment (somewhat like ''eval'', but there are differences.) '''Todo:''' Discuss this in detail.
|
|
</note>
|
|
|
|
==== Indirection ====
|
|
|
|
Arrays can be expanded indirectly using the indirect parameter expansion syntax. Parameters whose values are of the form: ''name[index]'', ''name[@]'', or ''name[*]'' when expanded indirectly produce the expected results. This is mainly useful for passing arrays (especially multiple arrays) by name to a function.
|
|
|
|
This example is an "isSubset"-like predicate which returns true if all key-value pairs of the array given as the first argument to isSubset correspond to a key-value of the array given as the second argument. It demonstrates both indirect array expansion and indirect key-passing without eval using the aforementioned special compound assignment expansion.
|
|
<code>
|
|
isSubset() {
|
|
local -a 'xkeys=("${!'"$1"'[@]}")' 'ykeys=("${!'"$2"'[@]}")'
|
|
set -- "${@/%/[key]}"
|
|
|
|
(( ${#xkeys[@]} <= ${#ykeys[@]} )) || return 1
|
|
|
|
local key
|
|
for key in "${xkeys[@]}"; do
|
|
[[ ${!2+_} && ${!1} == ${!2} ]] || return 1
|
|
done
|
|
}
|
|
|
|
main() {
|
|
# "a" is a subset of "b"
|
|
local -a 'a=({0..5})' 'b=({0..10})'
|
|
isSubset a b
|
|
echo $? # true
|
|
|
|
# "a" contains a key not in "b"
|
|
local -a 'a=([5]=5 {6..11})' 'b=({0..10})'
|
|
isSubset a b
|
|
echo $? # false
|
|
|
|
# "a" contains an element whose value != the corresponding member of "b"
|
|
local -a 'a=([5]=5 6 8 9 10)' 'b=({0..10})'
|
|
isSubset a b
|
|
echo $? # false
|
|
}
|
|
|
|
main
|
|
</code>
|
|
|
|
This script is one way of implementing a crude multidimensional associative array by storing array definitions in an array and referencing them through indirection. The script takes two keys and dynamically calls a function whose name is resolved from the array.
|
|
<code>
|
|
callFuncs() {
|
|
# Set up indirect references as positional parameters to minimize local name collisions.
|
|
set -- "${@:1:3}" ${2+'a["$1"]' "$1"'["$2"]'}
|
|
|
|
# The only way to test for set but null parameters is unfortunately to test each individually.
|
|
local x
|
|
for x; do
|
|
[[ $x ]] || return 0
|
|
done
|
|
|
|
local -A a=(
|
|
[foo]='([r]=f [s]=g [t]=h)'
|
|
[bar]='([u]=i [v]=j [w]=k)'
|
|
[baz]='([x]=l [y]=m [z]=n)'
|
|
) ${4+${a["$1"]+"${1}=${!3}"}} # For example, if "$1" is "bar" then define a new array: bar=([u]=i [v]=j [w]=k)
|
|
|
|
${4+${a["$1"]+"${!4-:}"}} # Now just lookup the new array. for inputs: "bar" "v", the function named "j" will be called, which prints "j" to stdout.
|
|
}
|
|
|
|
main() {
|
|
# Define functions named {f..n} which just print their own names.
|
|
local fun='() { echo "$FUNCNAME"; }' x
|
|
|
|
for x in {f..n}; do
|
|
eval "${x}${fun}"
|
|
done
|
|
|
|
callFuncs "$@"
|
|
}
|
|
|
|
main "$@"
|
|
</code>
|
|
|
|
===== Bugs and Portability Considerations =====
|
|
|
|
* Arrays are not specified by POSIX. One-dimensional indexed arrays are supported using similar syntax and semantics by most Korn-like shells.
|
|
* Associative arrays are supported via ''typeset -A'' in Bash 4, Zsh, and Ksh93.
|
|
* In Ksh93, arrays whose types are not given explicitly are not necessarily indexed. Arrays defined using compound assignments which specify subscripts are associative by default. In Bash, associative arrays can //only// be created by explicitly declaring them as associative, otherwise they are always indexed. In addition, ksh93 has several other compound structures whose types can be determined by the compound assignment syntax used to create them.
|
|
* In Ksh93, using the ''='' compound assignment operator unsets the array, including any attributes that have been set on the array prior to assignment. In order to preserve attributes, you must use the ''+='' operator. However, declaring an associative array, then attempting an ''a=(...)'' style compound assignment without specifying indexes is an error. I can't explain this inconsistency.<code>
|
|
$ ksh -c 'function f { typeset -a a; a=([0]=foo [1]=bar); typeset -p a; }; f' # Attribute is lost, and since subscripts are given, we default to associative.
|
|
typeset -A a=([0]=foo [1]=bar)
|
|
$ ksh -c 'function f { typeset -a a; a+=([0]=foo [1]=bar); typeset -p a; }; f' # Now using += gives us the expected results.
|
|
typeset -a a=(foo bar)
|
|
$ ksh -c 'function f { typeset -A a; a=(foo bar); typeset -p a; }; f' # On top of that, the reverse does NOT unset the attribute. No idea why.
|
|
ksh: f: line 1: cannot append index array to associative array a
|
|
</code>
|
|
* Only Bash and mksh support compound assignment with mixed explicit subscripts and automatically incrementing subscripts. In ksh93, in order to specify individual subscripts within a compound assignment, all subscripts must be given (or none). Zsh doesn't support specifying individual subscripts at all.
|
|
* Appending to a compound assignment is a fairly portable way to append elements after the last index of an array. In Bash, this also sets append mode for all individual assignments within the compound assignment, such that if a lower subscript is specified, subsequent elements will be appended to previous values. In ksh93, it causes subscripts to be ignored, forcing appending everything after the last element. (Appending has different meaning due to support for multi-dimensional arrays and nested compound datastructures.) <code>
|
|
$ ksh -c 'function f { typeset -a a; a+=(foo bar baz); a+=([3]=blah [0]=bork [1]=blarg [2]=zooj); typeset -p a; }; f' # ksh93 forces appending to the array, disregarding subscripts
|
|
typeset -a a=(foo bar baz '[3]=blah' '[0]=bork' '[1]=blarg' '[2]=zooj')
|
|
$ bash -c 'function f { typeset -a a; a+=(foo bar baz); a+=(blah [0]=bork blarg zooj); typeset -p a; }; f' # Bash applies += to every individual subscript.
|
|
declare -a a='([0]="foobork" [1]="barblarg" [2]="bazzooj" [3]="blah")'
|
|
$ mksh -c 'function f { typeset -a a; a+=(foo bar baz); a+=(blah [0]=bork blarg zooj); typeset -p a; }; f' # Mksh does like Bash, but clobbers previous values rather than appending.
|
|
set -A a
|
|
typeset a[0]=bork
|
|
typeset a[1]=blarg
|
|
typeset a[2]=zooj
|
|
typeset a[3]=blah
|
|
</code>
|
|
* In Bash and Zsh, the alternate value assignment parameter expansion (''${arr[idx]:=foo}'') evaluates the subscript twice, first to determine whether to expand the alternate, and second to determine the index to assign the alternate to. See [[#evaluation_order | evaluation order]]. <code>
|
|
$ : ${_[$(echo $RANDOM >&2)1]:=$(echo hi >&2)}
|
|
13574
|
|
hi
|
|
14485
|
|
</code>
|
|
* In Zsh, arrays are indexed starting at 1 in its default mode. Emulation modes are required in order to get any kind of portability.
|
|
* Zsh and mksh do not support compound assignment arguments to ''typeset''.
|
|
* Ksh88 didn't support modern compound array assignment syntax. The original (and most portable) way to assign multiple elements is to use the ''set -A name arg1 arg2 ...'' syntax. This is supported by almost all shells that support ksh-like arrays except for Bash. Additionally, these shells usually support an optional ''-s'' argument to ''set'' which performs lexicographic sorting on either array elements or the positional parameters. Bash has no built-in sorting ability other than the usual comparison operators. <code>
|
|
$ ksh -c 'set -A arr -- foo bar bork baz; typeset -p arr' # Classic array assignment syntax
|
|
typeset -a arr=(foo bar bork baz)
|
|
$ ksh -c 'set -sA arr -- foo bar bork baz; typeset -p arr' # Native sorting!
|
|
typeset -a arr=(bar baz bork foo)
|
|
$ mksh -c 'set -sA arr -- foo "[3]=bar" "[2]=baz" "[7]=bork"; typeset -p arr' # Probably a bug. I think the maintainer is aware of it.
|
|
set -A arr
|
|
typeset arr[2]=baz
|
|
typeset arr[3]=bar
|
|
typeset arr[7]=bork
|
|
typeset arr[8]=foo
|
|
</code>
|
|
* Evaluation order for assignments involving arrays varies significantly depending on context. Notably, the order of evaluating the subscript or the value first can change in almost every shell for both expansions and arithmetic variables. See [[#evaluation_order | evaluation order]] for details.
|
|
* Bash 4.1.* and below cannot use negative subscripts to address array indexes relative to the highest-numbered index. You must use the subscript expansion, i.e. ''"${arr[@]:(-n):1}"'', to expand the nth-last element (or the next-highest indexed after ''n'' if ''arr[n]'' is unset). In Bash 4.2, you may expand (but not assign to) a negative index. In Bash 4.3, ksh93, and zsh, you may both assign and expand negative offsets.
|
|
* ksh93 also has an additional slice notation: ''"${arr[n..m]}"'' where ''n'' and ''m'' are arithmetic expressions. These are needed for use with multi-dimensional arrays.
|
|
* Assigning or referencing negative indexes in mksh causes wrap-around. The max index appears to be ''UINT_MAX'', which would be addressed by ''arr[-1]''.
|
|
* So far, Bash's ''-v var'' test doesn't support individual array subscripts. You may supply an array name to test whether an array is defined, but can't check an element. ksh93's ''-v'' supports both. Other shells lack a ''-v'' test.
|
|
|
|
==== Bugs ====
|
|
|
|
* **Fixed in 4.3** Bash 4.2.* and earlier considers each chunk of a compound assignment, including the subscript for globbing. The subscript part is considered quoted, but any unquoted glob characters on the right-hand side of the ''[...]='' will be clumped with the subscript and counted as a glob. Therefore, you must quote anything on the right of the ''='' sign. This is fixed in 4.3, so that each subscript assignment statement is expanded following the same rules as an ordinary assignment. This also works correctly in ksh93. <code>
|
|
$ touch '[1]=a'; bash -c 'a=([1]=*); echo "${a[@]}"'
|
|
[1]=a
|
|
</code> mksh has a similar but even worse problem in that the entire subscript is considered a glob. <code>
|
|
$ touch 1=a; mksh -c 'a=([123]=*); print -r -- "${a[@]}"'
|
|
1=a
|
|
</code>
|
|
* **Fixed in 4.3** In addition to the above globbing issue, assignments preceding "declare" have an additional effect on brace and pathname expansion. <code>
|
|
$ set -x; foo=bar declare arr=( {1..10} )
|
|
+ foo=bar
|
|
+ declare 'arr=(1)' 'arr=(2)' 'arr=(3)' 'arr=(4)' 'arr=(5)' 'arr=(6)' 'arr=(7)' 'arr=(8)' 'arr=(9)' 'arr=(10)'
|
|
|
|
$ touch xy=foo
|
|
+ touch xy=foo
|
|
$ declare x[y]=*
|
|
+ declare 'x[y]=*'
|
|
$ foo=bar declare x[y]=*
|
|
+ foo=bar
|
|
+ declare xy=foo
|
|
</code> Each word (the entire assignment) is subject to globbing and brace expansion. This appears to trigger the same strange expansion mode as ''let'', ''eval'', other declaration commands, and maybe more.
|
|
* **Fixed in 4.3** Indirection combined with another modifier expands arrays to a single word. <code>
|
|
$ a=({a..c}) b=a[@]; printf '<%s> ' "${!b}"; echo; printf '<%s> ' "${!b/%/foo}"; echo
|
|
<a> <b> <c>
|
|
<a b cfoo>
|
|
</code>
|
|
* **Fixed in 4.3** Process substitutions are evaluated within array indexes. Zsh and ksh don't do this in any arithmetic context. <code>
|
|
# print "moo"
|
|
dev=fd=1 _[1<(echo moo >&2)]=
|
|
|
|
# Fork bomb
|
|
${dev[${dev='dev[1>(${dev[dev]})]'}]}
|
|
</code>
|
|
|
|
==== Evaluation order ====
|
|
|
|
Here are some of the nasty details of array assignment evaluation order. You can use this [[https://gist.github.com/ormaaj/4942297 | testcase code]] to generate these results.
|
|
|
|
<code>
|
|
Each testcase prints evaluation order for indexed array assignment
|
|
contexts. Each context is tested for expansions (represented by digits) and
|
|
arithmetic (letters), ordered from left to right within the expression. The
|
|
output corresponds to the way evaluation is re-ordered for each shell:
|
|
|
|
a[ $1 a ]=${b[ $2 b ]:=${c[ $3 c ]}} No attributes
|
|
a[ $1 a ]=${b[ $2 b ]:=c[ $3 c ]} typeset -ia a
|
|
a[ $1 a ]=${b[ $2 b ]:=c[ $3 c ]} typeset -ia b
|
|
a[ $1 a ]=${b[ $2 b ]:=c[ $3 c ]} typeset -ia a b
|
|
(( a[ $1 a ] = b[ $2 b ] ${c[ $3 c ]} )) No attributes
|
|
(( a[ $1 a ] = ${b[ $2 b ]:=c[ $3 c ]} )) typeset -ia b
|
|
a+=( [ $1 a ]=${b[ $2 b ]:=${c[ $3 c ]}} [ $4 d ]=$(( $5 e )) ) typeset -a a
|
|
a+=( [ $1 a ]=${b[ $2 b ]:=c[ $3 c ]} [ $4 d ]=${5}e ) typeset -ia a
|
|
|
|
bash: 4.2.42(1)-release
|
|
2 b 3 c 2 b 1 a
|
|
2 b 3 2 b 1 a c
|
|
2 b 3 2 b c 1 a
|
|
2 b 3 2 b c 1 a c
|
|
1 2 3 c b a
|
|
1 2 b 3 2 b c c a
|
|
1 2 b 3 c 2 b 4 5 e a d
|
|
1 2 b 3 2 b 4 5 a c d e
|
|
|
|
ksh93: Version AJM 93v- 2013-02-22
|
|
1 2 b b a
|
|
1 2 b b a
|
|
1 2 b b a
|
|
1 2 b b a
|
|
1 2 3 c b a
|
|
1 2 b b a
|
|
1 2 b b a 4 5 e d
|
|
1 2 b b a 4 5 d e
|
|
|
|
mksh: @(#)MIRBSD KSH R44 2013/02/24
|
|
2 b 3 c 1 a
|
|
2 b 3 1 a c
|
|
2 b 3 c 1 a
|
|
2 b 3 c 1 a
|
|
1 2 3 c a b
|
|
1 2 b 3 c a
|
|
1 2 b 3 c 4 5 e a d
|
|
1 2 b 3 4 5 a c d e
|
|
|
|
zsh: 5.0.2
|
|
2 b 3 c 2 b 1 a
|
|
2 b 3 2 b 1 a c
|
|
2 b 1 a
|
|
2 b 1 a
|
|
1 2 3 c b a
|
|
1 2 b a
|
|
1 2 b 3 c 2 b 4 5 e
|
|
1 2 b 3 2 b 4 5
|
|
</code>
|
|
|
|
===== See also =====
|
|
|
|
* [[syntax:pe|Parameter expansion]] (contains sections for arrays)
|
|
* [[syntax:ccmd:classic_for]] (contains some examples to iterate over arrays)
|
|
* [[commands:builtin:declare]]
|
|
* [[http://mywiki.wooledge.org/BashFAQ/005|BashFAQ 005 - How can I use array variables?]] - A very detailed discussion on arrays with many examples.
|
|
* [[http://mywiki.wooledge.org/BashSheet#Arrays|BashSheet - Arrays]] - Bashsheet quick-reference on Greycat's wiki.
|
|
|
|
<div hide> vim: set fenc=utf-8 ff=unix ts=4 sts=4 sw=4 ft=dokuwiki et wrap lbr: </div></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="syntax/arrays.txt">syntax/arrays.txt</span></li><li><span class="iconify text-muted" data-icon="mdi:calendar"></span> Last modified: <span title="2019/11/03 16:45">2019/11/03 16:45</span></li><li class="text-muted">by <bdi>ersen</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/20230325102835/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/20230325102835js_/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/20230325102835im_/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/20230325102835/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/20230325102835im_/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/20230325102835/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/20230325102835im_/https://wiki.bash-hackers.org/lib/exe/indexer.php?id=syntax%3Aarrays&1679740120" width="2" height="1" alt=""/>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|
|
<!--
|
|
FILE ARCHIVED ON 10:28:35 Mar 25, 2023 AND RETRIEVED FROM THE
|
|
INTERNET ARCHIVE ON 11:36:18 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: 141.839
|
|
exclusion.robots: 0.169
|
|
exclusion.robots.policy: 0.159
|
|
RedisCDXSource: 0.527
|
|
esindex: 0.007
|
|
LoadShardBlock: 115.23 (3)
|
|
PetaboxLoader3.datanode: 102.445 (4)
|
|
load_resource: 67.11
|
|
PetaboxLoader3.resolve: 29.308
|
|
--> |