Poop Sheet

Search and Replace

#!/usr/bin/bash

shopt -s extglob

ExampleGroup 'Search and replace'

  Example '${parameter/pattern/string} The first occurrence of pattern will be replaced by string.'
    str="linux is awesome to work with."
    When call echo "${str/linux/LINUX}"
    The output should eq "LINUX is awesome to work with."
  End

  Example 'unescaped & replaces & with matched string'
    var=abcdef
    When call echo ${var/abc/& }
    The output should eq "abc def"
  End

  Example 'escaped & keeps symbol'
    var=abcdef
    When call echo ${var/abc/\& }
    The output should eq "& def"
  End

  Example 'double quoted & also keeps symbol'
    var=abcdef
    When call echo ${var/abc/"& "}
    The output should eq "& def"
  End

End