Skip to content

Commit 187e5a1

Browse files
committed
Add reverse case function
1 parent 62bbe5a commit 187e5a1

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,32 @@ $ upper "HELLO"
386386
HELLO
387387
```
388388
389+
## Reverse a string case
390+
391+
**CAVEAT:** Requires `bash` 4+
392+
393+
**Example Function:**
394+
395+
```sh
396+
reverse_case() {
397+
# Usage: reverse_case "string"
398+
printf '%s\n' "${1~~}"
399+
}
400+
```
401+
402+
**Example Usage:**
403+
404+
```shell
405+
$ reverse_case "hello"
406+
HELLO
407+
408+
$ reverse_case "HeLlO"
409+
hElLo
410+
411+
$ reverse_case "HELLO"
412+
hello
413+
```
414+
389415
## Trim quotes from a string
390416
391417
**Example Function:**
@@ -1221,6 +1247,8 @@ Contrary to popular belief, there is no issue in utilizing raw escape sequences.
12211247
| `${VAR^^}` | Uppercase all characters. | `bash 4+` |
12221248
| `${VAR,}` | Lowercase first character. | `bash 4+` |
12231249
| `${VAR,,}` | Lowercase all characters. | `bash 4+` |
1250+
| `${VAR~}` | Reverse case of first character. | `bash 4+` |
1251+
| `${VAR~~}` | Reverse case of all characters. | `bash 4+` |
12241252
12251253
12261254
## Default Value

manuscript/chapter1.txt

+26
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,32 @@ $ upper "HELLO"
209209
HELLO
210210
```
211211

212+
## Reverse a string case
213+
214+
**CAVEAT:** Requires `bash` 4+
215+
216+
**Example Function:**
217+
218+
```sh
219+
reverse_case() {
220+
# Usage: reverse_case "string"
221+
printf '%s\n' "${1~~}"
222+
}
223+
```
224+
225+
**Example Usage:**
226+
227+
```shell
228+
$ reverse_case "hello"
229+
HELLO
230+
231+
$ reverse_case "HeLlO"
232+
hElLo
233+
234+
$ reverse_case "HELLO"
235+
hello
236+
```
237+
212238
## Trim quotes from a string
213239

214240
**Example Function:**

manuscript/chapter8.txt

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
| `${VAR^^}` | Uppercase all characters. | `bash 4+` |
4949
| `${VAR,}` | Lowercase first character. | `bash 4+` |
5050
| `${VAR,,}` | Lowercase all characters. | `bash 4+` |
51+
| `${VAR~}` | Reverse case of first character. | `bash 4+` |
52+
| `${VAR~~}` | Reverse case of all characters. | `bash 4+` |
5153

5254

5355
## Default Value

test.sh

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ test_upper() {
2828
assert_equals "$result" "HELLO"
2929
}
3030

31+
test_reverse_case() {
32+
result="$(reverse_case "HeLlO")"
33+
assert_equals "$result" "hElLo"
34+
}
35+
3136
test_trim_quotes() {
3237
result="$(trim_quotes "\"te'st' 'str'ing\"")"
3338
assert_equals "$result" "test string"

0 commit comments

Comments
 (0)