File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -75,3 +75,81 @@ rm -f $0
75
75
```
76
76
77
77
You need to be careful with the self deletion and ensure that you have your script backed up before you self-delete it.
78
+
79
+
80
+ ## Substring in Bash :: Slicing
81
+
82
+ Let's review the following example of slicing in a string in Bash:
83
+
84
+ ``` bash
85
+ #! /bin/bash
86
+
87
+ letters=( " A" " B" " C" " D" " E" )
88
+ echo ${letters[@]}
89
+ ```
90
+
91
+ This command will print all the elements of an array.
92
+
93
+ Output:
94
+
95
+ ``` bash
96
+ $ ABCDE
97
+ ```
98
+
99
+
100
+ Lets see a few more examples:
101
+
102
+ - Example 1
103
+
104
+ ``` bash
105
+ #! /bin/bash
106
+
107
+ letters=( " A" " B" " C" " D" " E" )
108
+ b=${letters: 0: 2}
109
+ echo " ${b} "
110
+ ```
111
+
112
+ This command wil print array from starting index 0 to 2 where 2 is exclusive.
113
+
114
+ ``` bash
115
+ $ AB
116
+ ```
117
+
118
+ - Example 2
119
+
120
+ ``` bash
121
+ #! /bin/bash
122
+
123
+ letters=( " A" " B" " C" " D" " E" )
124
+ b=${letters:: 5}
125
+ echo " ${b} "
126
+ ```
127
+
128
+ This command will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0 .
129
+
130
+ ``` bash
131
+ $ ABCDE
132
+ ```
133
+
134
+ - Example 3
135
+
136
+ ``` bash
137
+ #! /bin/bash
138
+
139
+ letters=( " A" " B" " C" " D" " E" )
140
+ b=${letters: 3}
141
+ echo " ${b} "
142
+ ```
143
+
144
+ This command will print from starting index
145
+ 3 to end of array inclusive .
146
+
147
+ ``` bash
148
+ $ DE
149
+ ```
150
+
151
+
152
+
153
+
154
+
155
+
You can’t perform that action at this time.
0 commit comments