{"id":44,"date":"2024-02-18T23:18:23","date_gmt":"2024-02-18T23:18:23","guid":{"rendered":"http:\/\/courses.usnh.domains\/cms\/?p=44"},"modified":"2024-02-18T23:18:23","modified_gmt":"2024-02-18T23:18:23","slug":"arrays","status":"publish","type":"post","link":"https:\/\/courses.usnh.domains\/cms\/it-612\/arrays\/","title":{"rendered":"Arrays"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Bash Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you have ever done any programming, you are probably already familiar with arrays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But just in case you are not a developer, the main thing that you need to know is that unlike variables, arrays can hold several values under one name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can initialize an array by assigning values devided by space and enclosed in <code>()<\/code>. Example: my_array=(&#8220;value 1&#8221; &#8220;value 2&#8221; &#8220;value 3&#8221; &#8220;value 4&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To access the elements in the array, you need to reference them by their numeric index.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">{notice} keep in mind that you need to use curly brackets.<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Access a single element, this would output: <code>value 2<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">echo ${my_array[1]}<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This would return the last element: <code>value 4<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">echo ${my_array[-1]}<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As with command line arguments using <code>@<\/code> will return all arguments in the array, as follows: <code>value 1 value 2 value 3 value 4<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">echo ${my_array[@]}<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prepending the array with a hash sign (<code>#<\/code>) would output the total number of elements in the array, in our case it is <code>4<\/code>:<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">echo ${#my_array[@]}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure to test this and practice it at your end with different values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\/blob\/main\/ebook\/en\/content\/008-bash-arrays.md#substring-in-bash--slicing\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Substring in Bash :: Slicing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s review the following example of slicing in a string in Bash: #!\/bin\/bash letters=( &#8220;A&#8221;&#8221;B&#8221;&#8221;C&#8221;&#8221;D&#8221;&#8221;E&#8221; ) echo ${letters[@]}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command will print all the elements of an array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Output: $ ABCDE<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see a few more examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 1<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">#!\/bin\/bash letters=( &#8220;A&#8221;&#8221;B&#8221;&#8221;C&#8221;&#8221;D&#8221;&#8221;E&#8221; ) b=${letters:0:2} echo &#8220;${b}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command will print array from starting index 0 to 2 where 2 is exclusive. $ AB<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">#!\/bin\/bash letters=( &#8220;A&#8221;&#8221;B&#8221;&#8221;C&#8221;&#8221;D&#8221;&#8221;E&#8221; ) b=${letters::5} echo &#8220;${b}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0 . $ ABCDE<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Example 3<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">#!\/bin\/bash letters=( &#8220;A&#8221;&#8221;B&#8221;&#8221;C&#8221;&#8221;D&#8221;&#8221;E&#8221; ) b=${letters:3} echo &#8220;${b}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command will print from starting index 3 to end of array inclusive . $ DE<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash Arrays If you have ever done any programming, you are probably already familiar with arrays. But just in case you are not a developer, the main thing that you need to know is that unlike variables, arrays can hold several values under one name. You can initialize an array by assigning values devided by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-bash","category-it-612"],"_links":{"self":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":1,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/44\/revisions\/45"}],"wp:attachment":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}