echo "${#MY_STRING}" MY_ARR=($MY_STRING)
echo ${MY_ARR[@]}
for element in ${MY_ARR[@]}; do
echo $element
doneSTR_TO_ARR="column1,column2,column3"
IFS=","
MY_ARR=(${STR_TO_ARR})
for element in ${MY_ARR[@]}; do echo $element; done
echo "${MY_ARR[@]}"| Symbol | Conversion Type |
|---|---|
| ,, | Converts to lowercase |
| ^^ | Converts to uppercase |
| ~~ | Converts to transpose Case |
| , | Converts 1st letter to lowercase |
| ^ | Converts 1st letter to uppercase |
lower_to_UPPER="welcome to the funzone"
echo ${lower_to_UPPER^^}UPPER_to_lower="WELCOME TO THE FUNZONE"
echo ${UPPER_to_lower,,}TRS_CASE="Welcome To The FunZone"
echo ${TRS_CASE~~}First_to_lower="FUNZONE"
echo ${First_to_lower,}First_to_UPPER="FUNZONE"
echo ${First_to_UPPER^}lower_to_UPPER="welcome to the funzone"
echo ${lower_to_UPPER^^[fun]}name="John Doe"
echo ${name:l}
echo ${name:u}
# You can also use expansion flags:
name="John Doe"
echo ${(L)name}
echo ${(U)name}In zsh you can even declare a variable as inherently lower case or upper case
This will not affect the contents of the variable, but it will automatically be lower- or upper- cased on expansion:
typeset -l name
name="John Doe"
echo $name
typeset -u name
echo $name MY_STRING="the FunZone"
MY_STRING2="Welcome to"
echo "${MY_STRING2} {STRING1}"# {STRING:START:LENGTH}
# START => Starting Index Position
# LENGTH => Length to slice
# If LENGTH is not specified: prints from START to end of string
MY_STRING="Foobar"
echo ${MY_STRING:2}
echo ${MY_STRING:2:2}# native way to search and replace characters in a string
# {STRING/X/Y}
# The first occurrence of X will be replaced by Y
MY_STRING="Foobar is not a Foobar but it sometimes can be a Foobar"
echo $MY_STRING
echo ${MY_STRING/Foo/fOO}
echo $MY_STRING
# replace all the occurrences of the string
echo ${MY_STRING//Foo/fOO}# remove everything after the first period "."
# This will match the last found pattern and remove it
MY_STRING="www.disney.com"
echo ${MY_STRING%.*}
# To match the first occurrence of the pattern, use double percentage symbol
MY_STRING="www.disney.com"
echo ${MY_STRING%%.*}
# We can use the # or ## symbols to do the inverse
# With a single # symbol, the first pattern is matched and everything before the pattern is deleted
MY_STRING="www.disney.com"
echo ${MY_STRING#.*}
# With the double ## symbol, the last pattern is matched and everything before the pattern is deleted
MY_STRING="www.disney.com"
echo ${MY_STRING##.*}