Annotated edit history of
ManipulatingStrings version 7, including all changes.
View license author blame.
Rev |
Author |
# |
Line |
1 |
BenStaz |
1 |
!Two Methods For Finding The Length Of a String |
|
|
2 |
|
|
|
3 |
*test="This is a string" |
|
|
4 |
|
|
|
5 |
Method 1) |
|
|
6 |
|
|
|
7 |
*echo ${#test} |
|
|
8 |
|
|
|
9 |
Method 2) |
|
|
10 |
|
|
|
11 |
*expr length test |
|
|
12 |
|
|
|
13 |
Both will give you the correct length of 16. |
3 |
BenStaz |
14 |
|
2 |
BenStaz |
15 |
|
|
|
16 |
!How can I get a substring of a variable? |
|
|
17 |
|
|
|
18 |
Say the variable test contains the string "corndog" but we want just the "dog" part. |
|
|
19 |
|
|
|
20 |
echo ${test:4:3} will return just "dog" |
|
|
21 |
|
|
|
22 |
The 4 tells us the index of the char that will be the first char in the substring. The 3 tells us how many letters to add starting at the index. |
4 |
BenStaz |
23 |
|
5 |
BenStaz |
24 |
!Remove Substrings (uses bash's pattern matching) |
4 |
BenStaz |
25 |
|
|
|
26 |
Strips *shortest* match of $substring from *front* of $string. |
|
|
27 |
*${string#substring} |
|
|
28 |
|
|
|
29 |
For example if $string contained "This is a string" and we replaced substring with ''T*'' an echo would result in |
|
|
30 |
''his is a string'' |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
Strips *longest* match of $substring from *front* of $string. |
|
|
35 |
*${string##substring} |
|
|
36 |
|
|
|
37 |
For example if $string contained "This is a string" and we replaced substring with ''T*'' an echo would result in an empty string. (The longest match was the entire string). |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
Strips *shortest* match of $substring from *back* of $string. |
|
|
42 |
*${string%substring} |
|
|
43 |
|
|
|
44 |
For example if $string contained "This is a string" and we replaced substring with ''s*'' an echo would result in |
|
|
45 |
''This is a'' |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
Strips *longest* match of $substring from *back* of $string. |
|
|
50 |
*${string%%substring} |
|
|
51 |
|
|
|
52 |
For example if $string contained "This is a string" and we replaced substring with ''s*'' an echo would result in |
|
|
53 |
''Thi'' (The longest match) |
6 |
BenStaz |
54 |
|
|
|
55 |
|
|
|
56 |
!Concatenate variables. |
|
|
57 |
|
|
|
58 |
*echo ${string1}${string2} |
|
|
59 |
|
|
|
60 |
If we did: |
|
|
61 |
|
|
|
62 |
*echo $string1string2 |
|
|
63 |
|
|
|
64 |
then bash would be looking for a variable called ''string1string2'' (which does not exist). This is why the curly brackets are necessary. |
|
|
65 |
|
|
|
66 |
Another Example: |
|
|
67 |
|
|
|
68 |
*echo ${string1}"Some more Text" |
7 |
BenStaz |
69 |
|
|
|
70 |
!Brace Expansion |
|
|
71 |
|
|
|
72 |
*echo C{a,u,o}t |
|
|
73 |
|
|
|
74 |
Will return ''Cat Cut Cot'' |
|
|
75 |
|
|
|
76 |
Another example of where this could be useful: |
|
|
77 |
|
|
|
78 |
*mkdir ~~/test/{cat,dog,rabbit} |
|
|
79 |
|
|
|
80 |
This will create a ''cat'' , ''dog'' and ''rabbit'' dir in ~~/test/ |