Skip to content

Commit a7c59dc

Browse files
committed
Add headings
1 parent 65209e4 commit a7c59dc

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

Diff for: content/posts/2020-09-17-zsh-splitting-a-string-into-an-array.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@ categories = 'Guide'
55
tags = ['Zsh', 'Shell']
66
+++
77

8+
# Zsh - splitting a string into an array
9+
10+
## Intro
11+
812
There are a ton of different ways to split up a string in Zsh. This post attempts to
913
show them with examples to help you build your own. I write Zsh scripts all the time,
1014
and still reference back to this guide, so there you go.
1115

16+
## Using the split parameter expansion `${(s/x/)foo}`
17+
1218
From the Zsh docs on [Parameter Expansion
1319
Flags](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion-Flags)
1420
(yeah - I know... how would anyone ever find that if they didn't know where to look!?)
1521

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.
1825
1926
You can also read more by running `man zshexpn`. (Again... I know, right!? How would
2027
anyone know to look there!?)
2128

22-
Example splitting a string on slash character in Zsh.
29+
Example splitting a string on slash character in Zsh:
2330

2431
```zsh
2532
$ str=part1/part2/part3
@@ -30,6 +37,8 @@ $ echo ${#parts[@]}
3037
3
3138
```
3239

40+
## Using split with bracketed indexing `${foo[start,end]}`
41+
3342
You can use split `${(@s:/:)str}` and indexing `[start,end]` to do more sophisticated
3443
surgery on string parts. Note that Zsh array indexing starts a 1, not 0.
3544

@@ -48,7 +57,7 @@ sorin-ionescu/prezto
4857
[An example from the Zsh
4958
docs](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Examples) which shows
5059
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
5261
split at all:
5362

5463
```zsh
@@ -59,7 +68,7 @@ a
5968
1
6069
```
6170

62-
Getting the first 2 parts
71+
Getting the first 2 parts:
6372

6473
```zsh
6574
$ str=a/b/c/d/e/f
@@ -68,6 +77,8 @@ $ echo ${(@j:/:)parts[1,2]}
6877
a/b
6978
```
7079

80+
## Pattern removal with `#` and `%`
81+
7182
You can also use `#` and `%` parameter expansion symbols: [see
7283
docs](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion). `#`
7384
removes from the left side, and `%` from the right, which I remember by the fact that
@@ -88,7 +99,11 @@ $ echo ${str##*/}
8899
part3
89100
```
90101

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:
92107

93108
```zsh
94109
$ sentence="ls -l -A -h"
@@ -100,5 +115,7 @@ ls
100115
-h
101116
```
102117

118+
## Conclusion
119+
103120
Hope this is a helpful reference. There's [also a gist that I keep up-to-date as well
104121
here](https://gist.github.com/mattmc3/76ad634f362b5a9a54f1779a4737d5ae).

0 commit comments

Comments
 (0)