-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-expansion-modifiers-and-flags.sh
76 lines (56 loc) · 1.52 KB
/
zsh-expansion-modifiers-and-flags.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/zsh
str="Dir/subdir/subsubdir/File Name.extension"
echo $str "\n\n"
## PATH MANIPULATIONS
# turn a file name into an absolute path
echo ${str:a}
# -> $(pwd)/Dir/File Name.extension
# leave only the head (remove all trailing pathname components)
echo ${str:h}
# -> Dir/subdir
# remove all but 2 leading path components
echo ${str:h2}
# -> Dir/subdir
# leave only the tail (remove all leading pathname components)
echo ${str:t}
# -> File Name.extension
# Remove all but 2 trailing components
echo ${str:t2}
# -> File Name.extension
# remove all but the extension
echo ${str:e}
# -> extension
# remove extension
echo ${str:r}
# -> File Name
# remove all but filename (without extension)
echo ${str:t:r}
# -> extension
## CASE MANIPULATION
# Convert string to all uppercase
echo ${str:u}
# -> DIR/FILE NAME.EXTENSION
# convert string to all lowercase
echo ${str:l}
# -> dir/file name.extension
## REMOVE / REPLACE
# remove first occurence of "ir"
echo ${str/ir/}
echo ${str:s/ir/}
# -> D/subdir/File Name.extension
# remove all occurence of "ir"
echo ${str:gs/ir/}
# -> Dir/File .extension
# replace first occurence of "ir" with "on"
echo ${str/ir/on}
echo ${str:s/ir/on}
# -> Don/subdir/File Name.extension
# replace all occurences of "ir" with "on"
echo ${str:gs/ir/on}
# -> Don/subdon/File Name.extension
# quote the substituted words, escaping further substitutions
echo ${str:q}
# -> Dir/subdir/File\ Name.extension
# remove a filename extension leaving the root name. ‘.’
echo ${str:r}
# -> Dir/subdir/subsubdir/File Name