Skip to content

Improve arrays chapter #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ebook/en/content/000-about-the-author.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
84 changes: 43 additions & 41 deletions ebook/en/content/008-bash-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}
Expand All @@ -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.