AnyCompiler

String Manipulation

Bash has no string methods to call — instead, ${...} parameter expansion syntax does the same jobs. ${#str} gives the length. ${str^^} converts to uppercase and ${str,,} to lowercase (a Bash 4+ feature). ${str/old/new} substitutes the first match of old with new.

str="Hello, World!"
echo "${#str}"
echo "${str^^}"
echo "${str,,}"
echo "${str/World/Bash}"
Output
13
HELLO, WORLD!
hello, world!
Hello, Bash!

Next: parameter expansion — stripping prefixes and suffixes, and supplying a default value for an unset variable.

Practice exercise

Given str="shell scripting", print its length, then print it fully uppercased.