From 5df218a9663b6d10c5be12b96b4932cca381e634 Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Sun, 4 Aug 2024 17:11:35 +0300 Subject: [PATCH] Improve arrays chapter (#156) --- ebook/en/content/000-about-the-author.md | 2 +- ebook/en/content/008-bash-arrays.md | 84 ++++++++++++------------ 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/ebook/en/content/000-about-the-author.md b/ebook/en/content/000-about-the-author.md index 1882201..34b5b86 100644 --- a/ebook/en/content/000-about-the-author.md +++ b/ebook/en/content/000-about-the-author.md @@ -1,6 +1,6 @@ # About the book -* **This version was published on Oct 30 2023** +* **This version was published on 02 08 2024** This is an open-source introduction to Bash scripting guide that will help you learn the basics of Bash scripting and start writing awesome Bash scripts that will help you automate your daily SysOps, DevOps, and Dev tasks. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Bash scripts to combine different Linux commands and automate tedious and repetitive daily tasks so that you can focus on more productive and fun things. diff --git a/ebook/en/content/008-bash-arrays.md b/ebook/en/content/008-bash-arrays.md index 568bc6d..6242198 100644 --- a/ebook/en/content/008-bash-arrays.md +++ b/ebook/en/content/008-bash-arrays.md @@ -26,7 +26,7 @@ echo ${my_array[1]} echo ${my_array[-1]} ``` -* As with command line arguments using `@` will return all arguments in the array, as follows: `value 1 value 2 value 3 value 4` +* As with command line arguments using `@` will return all elements in the array, as follows: `value 1 value 2 value 3 value 4` ```bash echo ${my_array[@]} @@ -40,73 +40,75 @@ echo ${#my_array[@]} Make sure to test this and practice it at your end with different values. -## Substring in Bash :: Slicing +## Array Slicing -Let's review the following example of slicing in a string in Bash: +While Bash doesn't support true array slicing, you can achieve similar results using a combination of array indexing and string slicing: ```bash #!/bin/bash -letters=( "A""B""C""D""E" ) -echo ${letters[@]} -``` +array=("A" "B" "C" "D" "E") -This command will print all the elements of an array. +# Print entire array +echo "${array[@]}" # Output: A B C D E -Output: +# Access a single element +echo "${array[1]}" # Output: B -```bash -$ ABCDE +# Print a range of elements (requires Bash 4.0+) +echo "${array[@]:1:3}" # Output: B C D + +# Print from an index to the end +echo "${array[@]:3}" # Output: D E ``` +When working with arrays, always use `[@]` to refer to all elements, and enclose the parameter expansion in quotes to preserve spaces in array elements. -Let's see a few more examples: +## String Slicing -- Example 1 +In Bash, you can extract portions of a string using slicing. The basic syntax is: ```bash -#!/bin/bash - -letters=( "A""B""C""D""E" ) -b=${letters:0:2} -echo "${b}" +${string:start:length} ``` -This command will print array from starting index 0 to 2 where 2 is exclusive. +Where: +- `start` is the starting index (0-based) +- `length` is the maximum number of characters to extract -```bash -$ AB -``` - - - Example 2 +Let's look at some examples: ```bash #!/bin/bash -letters=( "A""B""C""D""E" ) -b=${letters::5} -echo "${b}" -``` +text="ABCDE" -This command will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0 . +# Extract from index 0, maximum 2 characters +echo "${text:0:2}" # Output: AB -```bash -$ ABCDE +# Extract from index 3 to the end +echo "${text:3}" # Output: DE + +# Extract 3 characters starting from index 1 +echo "${text:1:3}" # Output: BCD + +# If length exceeds remaining characters, it stops at the end +echo "${text:3:3}" # Output: DE (only 2 characters available) ``` -- Example 3 +Note that the second number in the slice notation represents the maximum length of the extracted substring, not the ending index. This is different from some other programming languages like Python. In Bash, if you specify a length that would extend beyond the end of the string, it will simply stop at the end of the string without raising an error. + +For example: ```bash -#!/bin/bash +text="Hello, World!" -letters=( "A""B""C""D""E" ) -b=${letters:3} -echo "${b}" -``` +# Extract 5 characters starting from index 7 +echo "${text:7:5}" # Output: World -This command will print from starting index - 3 to end of array inclusive . +# Attempt to extract 10 characters starting from index 7 +# (even though only 6 characters remain) +echo "${text:7:10}" # Output: World! +``` - ```bash - $ DE - ``` +In the second example, even though we asked for 10 characters, Bash only returns the 6 available characters from index 7 to the end of the string. This behavior can be particularly useful when you're not sure of the exact length of the string you're working with.