Bash Parameter Expansion
Substrings
Section titled “Substrings”str="Hello, world! I am an example, used below."Remove from end
Section titled “Remove from end”# Hello, world! I am an example, used below.echo "${str%,*}"# Hello, world! I am an exampleRemove from end (greedy)
Section titled “Remove from end (greedy)”# Hello, world! I am an example, used below.echo "${str%%,*}"# HelloRemove from start
Section titled “Remove from start”# Hello, world! I am an example, used below.echo "${str#*, }"# world! I am an example, used below.Remove from start (greedy)
Section titled “Remove from start (greedy)”# Hello, world! I am an example, used below.echo "${str##*, }"# used below.Replace first occurrence
Section titled “Replace first occurrence”# Hello, world! I am an example, used below.echo "${str/,/_}"# Hello_ world! I am an example, used below.Replace all occurrences
Section titled “Replace all occurrences”# Hello, world! I am an example, used below.echo "${str//,/_}"# Hello_ world! I am an example_ used below.