Skip to content

Instantly share code, notes, and snippets.

@petergi
Last active May 9, 2023 12:51
Show Gist options
  • Select an option

  • Save petergi/c5655382d5ac17f64ff902356f73dd48 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/c5655382d5ac17f64ff902356f73dd48 to your computer and use it in GitHub Desktop.

To find the length of a string use the # symbol

echo "${#MY_STRING}" 

Converting strings to Array

MY_ARR=($MY_STRING)
echo ${MY_ARR[@]}
for element in ${MY_ARR[@]}; do
echo $element
done

Converting strings to Array (alternate method)

STR_TO_ARR="column1,column2,column3"
IFS=","
MY_ARR=(${STR_TO_ARR})
for element in ${MY_ARR[@]}; do echo $element; done
echo "${MY_ARR[@]}"

Case conversion

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 UPPERCASE

lower_to_UPPER="welcome to the funzone"
echo ${lower_to_UPPER^^}

UPPER to lowercase

UPPER_to_lower="WELCOME TO THE FUNZONE"
echo ${UPPER_to_lower,,}

tRANSPOSE cASE

TRS_CASE="Welcome To The FunZone"
echo ${TRS_CASE~~}

First Letter to Lowercase

First_to_lower="FUNZONE"
echo ${First_to_lower,}

First Letter to UPPERCASE

First_to_UPPER="FUNZONE"
echo ${First_to_UPPER^}

You can also use regex pattern matching and convert the case for the matches

lower_to_UPPER="welcome to the funzone"
echo ${lower_to_UPPER^^[fun]}

NOTES FOR ZSH

In zsh you can use expansion modifiers (the bash substitution will likely give your errors)

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        

String concatenation

MY_STRING="the FunZone"
MY_STRING2="Welcome to"
echo "${MY_STRING2} {STRING1}"

String slicing

# {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}

Search and Replace

# 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 a substring

# 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##.*}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment