strings in bash. variables are strings by default we are going to look at some ways to manipulate...
TRANSCRIPT
![Page 1: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/1.jpg)
Strings in BASH
![Page 2: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/2.jpg)
• Variables are strings by default• We are going to look at some ways to
manipulate strings in BASH
![Page 3: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/3.jpg)
String Length
${#string} expr length $string
These are the equivalent of strlen() in C.
expr "$string" : '.*' stringZ=abcABC123ABCabc
echo ${#stringZ} # 15echo `expr length $stringZ` # 15echo `expr "$stringZ" : '.*'` # 15
![Page 4: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/4.jpg)
Index
expr index $string $substring
Numerical position in $string of first character in $substring that matches.stringZ=abcABC123ABCabc# 123456 ...echo `expr index "$stringZ" C12` # 6 # C position.
echo `expr index "$stringZ" c` # 3
![Page 5: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/5.jpg)
Substring Extraction
${string:position} Extracts substring from $string at $position.
${string:position:length} Extracts $length characters of substring from $string at $position.
![Page 6: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/6.jpg)
stringZ=abcABC123ABCabc# 0123456789.....# 0-based indexing.
echo ${stringZ:0} # abcABC123ABCabcecho ${stringZ:1} # bcABC123ABCabcecho ${stringZ:7} # 23ABCabc
echo ${stringZ:7:3} # 23A # Three characters of substring.
# Is it possible to index from the right end of the string? echo ${stringZ:-4} # abcABC123ABCabc# Defaults to full string, as in ${parameter:-default}.# However . . .
echo ${stringZ:(-4)} # Cabc echo ${stringZ: -4} # Cabc
The position and length arguments can be "parameterized," that is, represented as a variable, rather than as a numerical constant.
![Page 7: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/7.jpg)
Substring Removal
${string#substring} Deletes shortest match of $substring from front of $string.${string##substring} Deletes longest match of $substring from front of $string.stringZ=abcABC123ABCabc# |----| shortest# |----------| longest
echo ${stringZ#a*C} # 123ABCabc# Strip out shortest match between 'a' and 'C'.
echo ${stringZ##a*C} # abc# Strip out longest match between 'a' and 'C'.
![Page 8: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/8.jpg)
Substring Replacement
${string/substring/replacement} Replace first match of $substring with $replacement.
${string//substring/replacement} Replace all matches of $substring with $replacement.
![Page 9: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/9.jpg)
stringZ=abcABC123ABCabc
echo ${stringZ/abc/xyz} # xyzABC123ABCabc # Replaces first match of 'abc' with 'xyz'.
echo ${stringZ//abc/xyz} # xyzABC123ABCxyz # Replaces all matches of 'abc' with # 'xyz'.
echo "$stringZ" # abcABC123ABCabc# The string itself is not altered!
# Can the match and replacement strings be parameterized?match=abcrepl=000echo ${stringZ/$match/$repl} # 000ABC123ABCabc# ^ ^ ^^^echo ${stringZ//$match/$repl} # 000ABC123ABC000# Yes! ^ ^ ^^^ ^^^
echo
# What happens if no $replacement string is supplied?echo ${stringZ/abc} # ABC123ABCabcecho ${stringZ//abc} # ABC123ABC# A simple deletion takes place.
![Page 10: Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH](https://reader036.vdocuments.us/reader036/viewer/2022082512/551bd9f9550346be588b59cb/html5/thumbnails/10.jpg)
Palindrome Assignment
• Write a BASH shell script to determine if a word, supplied as input from the user, is a palindrome or not. Remember a palindrome is a word that is the same backwards as it is forwards. For example: racecar, noon, mom are palindromes.
• Once you have accomplished this you can determine if a sentence is a palindrome – you have to strip off punctuation and spaces and then test to see if it is a palindrome. For example, Madam, I’m Adam, and Go hang a salami, I’m a lasagna hog, are both palindromes. Have fun!!