@@ -5,21 +5,28 @@ categories = 'Guide'
5
5
tags = [' Zsh' , ' Shell' ]
6
6
+++
7
7
8
+ # Zsh - splitting a string into an array
9
+
10
+ ## Intro
11
+
8
12
There are a ton of different ways to split up a string in Zsh. This post attempts to
9
13
show them with examples to help you build your own. I write Zsh scripts all the time,
10
14
and still reference back to this guide, so there you go.
11
15
16
+ ## Using the split parameter expansion ` ${(s/x/)foo} `
17
+
12
18
From the Zsh docs on [ Parameter Expansion
13
19
Flags] ( https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion-Flags )
14
20
(yeah - I know... how would anyone ever find that if they didn't know where to look!?)
15
21
16
- > j:string: Join the words of arrays together using string as a separator. s:string:
17
- > Force field splitting at the separator string.
22
+ > j:string: Join the words of arrays together using string as a separator.
23
+ >
24
+ > s:string: Force field splitting at the separator string.
18
25
19
26
You can also read more by running ` man zshexpn ` . (Again... I know, right!? How would
20
27
anyone know to look there!?)
21
28
22
- Example splitting a string on slash character in Zsh.
29
+ Example splitting a string on slash character in Zsh:
23
30
24
31
``` zsh
25
32
$ str=part1/part2/part3
@@ -30,6 +37,8 @@ $ echo ${#parts[@]}
30
37
3
31
38
```
32
39
40
+ ## Using split with bracketed indexing ` ${foo[start,end]} `
41
+
33
42
You can use split ` ${(@s:/:)str} ` and indexing ` [start,end] ` to do more sophisticated
34
43
surgery on string parts. Note that Zsh array indexing starts a 1, not 0.
35
44
@@ -48,7 +57,7 @@ sorin-ionescu/prezto
48
57
[ An example from the Zsh
49
58
docs] ( https://zsh.sourceforge.io/Doc/Release/Expansion.html#Examples ) which shows
50
59
splitting. Note the use of slash below unlike the colon above to surround the split
51
- character - remember that symbol is swapable and doesn't change the behavior of the
60
+ character - remember that symbol is swappable and doesn't change the behavior of the
52
61
split at all:
53
62
54
63
``` zsh
59
68
1
60
69
```
61
70
62
- Getting the first 2 parts
71
+ Getting the first 2 parts:
63
72
64
73
``` zsh
65
74
$ str=a/b/c/d/e/f
@@ -68,6 +77,8 @@ $ echo ${(@j:/:)parts[1,2]}
68
77
a/b
69
78
```
70
79
80
+ ## Pattern removal with ` # ` and ` % `
81
+
71
82
You can also use ` # ` and ` % ` parameter expansion symbols: [ see
72
83
docs] ( http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion ) . ` # `
73
84
removes from the left side, and ` % ` from the right, which I remember by the fact that
@@ -88,7 +99,11 @@ $ echo ${str##*/}
88
99
part3
89
100
```
90
101
91
- Word splitting is done with the '=' character:
102
+ ## Word splitting
103
+
104
+ Word splitting is done for every command you type to split out arguments. You can use
105
+ those same Zsh word splitting rules for your own splits if you are splitting on
106
+ whitespace. This is done with the '=' character:
92
107
93
108
``` zsh
94
109
$ sentence=" ls -l -A -h"
100
115
-h
101
116
```
102
117
118
+ ## Conclusion
119
+
103
120
Hope this is a helpful reference. There's [ also a gist that I keep up-to-date as well
104
121
here] ( https://gist.github.com/mattmc3/76ad634f362b5a9a54f1779a4737d5ae ) .
0 commit comments