You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ebook/en/content/000-about-the-author.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# About the book
2
2
3
-
***This version was published on Oct 30 2023**
3
+
***This version was published on 02 08 2024**
4
4
5
5
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.
Copy file name to clipboardExpand all lines: ebook/en/content/008-bash-arrays.md
+43-41Lines changed: 43 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ echo ${my_array[1]}
26
26
echo${my_array[-1]}
27
27
```
28
28
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`
30
30
31
31
```bash
32
32
echo${my_array[@]}
@@ -40,73 +40,75 @@ echo ${#my_array[@]}
40
40
41
41
Make sure to test this and practice it at your end with different values.
42
42
43
-
## Substring in Bash :: Slicing
43
+
## Array Slicing
44
44
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:
46
46
47
47
```bash
48
48
#!/bin/bash
49
49
50
-
letters=( "A""B""C""D""E" )
51
-
echo${letters[@]}
52
-
```
50
+
array=("A""B""C""D""E")
53
51
54
-
This command will print all the elements of an array.
52
+
# Print entire array
53
+
echo"${array[@]}"# Output: A B C D E
55
54
56
-
Output:
55
+
# Access a single element
56
+
echo"${array[1]}"# Output: B
57
57
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
60
63
```
61
64
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.
62
66
63
-
Let's see a few more examples:
67
+
## String Slicing
64
68
65
-
- Example 1
69
+
In Bash, you can extract portions of a string using slicing. The basic syntax is:
66
70
67
71
```bash
68
-
#!/bin/bash
69
-
70
-
letters=( "A""B""C""D""E" )
71
-
b=${letters:0:2}
72
-
echo"${b}"
72
+
${string:start:length}
73
73
```
74
74
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
76
78
77
-
```bash
78
-
$ AB
79
-
```
80
-
81
-
- Example 2
79
+
Let's look at some examples:
82
80
83
81
```bash
84
82
#!/bin/bash
85
83
86
-
letters=( "A""B""C""D""E" )
87
-
b=${letters::5}
88
-
echo"${b}"
89
-
```
84
+
text="ABCDE"
90
85
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
92
88
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)
95
97
```
96
98
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:
98
102
99
103
```bash
100
-
#!/bin/bash
104
+
text="Hello, World!"
101
105
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
106
108
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
+
```
109
113
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