Skip to content

Commit 5df218a

Browse files
authored
Improve arrays chapter (#156)
1 parent d21600d commit 5df218a

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

ebook/en/content/000-about-the-author.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# About the book
22

3-
* **This version was published on Oct 30 2023**
3+
* **This version was published on 02 08 2024**
44

55
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.
66

ebook/en/content/008-bash-arrays.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ echo ${my_array[1]}
2626
echo ${my_array[-1]}
2727
```
2828

29-
* As with command line arguments using `@` will return all arguments in the array, as follows: `value 1 value 2 value 3 value 4`
29+
* As with command line arguments using `@` will return all elements in the array, as follows: `value 1 value 2 value 3 value 4`
3030

3131
```bash
3232
echo ${my_array[@]}
@@ -40,73 +40,75 @@ echo ${#my_array[@]}
4040

4141
Make sure to test this and practice it at your end with different values.
4242

43-
## Substring in Bash :: Slicing
43+
## Array Slicing
4444

45-
Let's review the following example of slicing in a string in Bash:
45+
While Bash doesn't support true array slicing, you can achieve similar results using a combination of array indexing and string slicing:
4646

4747
```bash
4848
#!/bin/bash
4949

50-
letters=( "A""B""C""D""E" )
51-
echo ${letters[@]}
52-
```
50+
array=("A" "B" "C" "D" "E")
5351

54-
This command will print all the elements of an array.
52+
# Print entire array
53+
echo "${array[@]}" # Output: A B C D E
5554

56-
Output:
55+
# Access a single element
56+
echo "${array[1]}" # Output: B
5757

58-
```bash
59-
$ ABCDE
58+
# Print a range of elements (requires Bash 4.0+)
59+
echo "${array[@]:1:3}" # Output: B C D
60+
61+
# Print from an index to the end
62+
echo "${array[@]:3}" # Output: D E
6063
```
6164

65+
When working with arrays, always use `[@]` to refer to all elements, and enclose the parameter expansion in quotes to preserve spaces in array elements.
6266

63-
Let's see a few more examples:
67+
## String Slicing
6468

65-
- Example 1
69+
In Bash, you can extract portions of a string using slicing. The basic syntax is:
6670

6771
```bash
68-
#!/bin/bash
69-
70-
letters=( "A""B""C""D""E" )
71-
b=${letters:0:2}
72-
echo "${b}"
72+
${string:start:length}
7373
```
7474

75-
This command will print array from starting index 0 to 2 where 2 is exclusive.
75+
Where:
76+
- `start` is the starting index (0-based)
77+
- `length` is the maximum number of characters to extract
7678

77-
```bash
78-
$ AB
79-
```
80-
81-
- Example 2
79+
Let's look at some examples:
8280

8381
```bash
8482
#!/bin/bash
8583

86-
letters=( "A""B""C""D""E" )
87-
b=${letters::5}
88-
echo "${b}"
89-
```
84+
text="ABCDE"
9085

91-
This command will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0 .
86+
# Extract from index 0, maximum 2 characters
87+
echo "${text:0:2}" # Output: AB
9288

93-
```bash
94-
$ ABCDE
89+
# Extract from index 3 to the end
90+
echo "${text:3}" # Output: DE
91+
92+
# Extract 3 characters starting from index 1
93+
echo "${text:1:3}" # Output: BCD
94+
95+
# If length exceeds remaining characters, it stops at the end
96+
echo "${text:3:3}" # Output: DE (only 2 characters available)
9597
```
9698

97-
- Example 3
99+
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.
100+
101+
For example:
98102

99103
```bash
100-
#!/bin/bash
104+
text="Hello, World!"
101105

102-
letters=( "A""B""C""D""E" )
103-
b=${letters:3}
104-
echo "${b}"
105-
```
106+
# Extract 5 characters starting from index 7
107+
echo "${text:7:5}" # Output: World
106108

107-
This command will print from starting index
108-
3 to end of array inclusive .
109+
# Attempt to extract 10 characters starting from index 7
110+
# (even though only 6 characters remain)
111+
echo "${text:7:10}" # Output: World!
112+
```
109113

110-
```bash
111-
$ DE
112-
```
114+
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.

0 commit comments

Comments
 (0)