work with strings in bash

Join, cut, replace, change the case of and trim strings in a script, without other programs.

  • shell
  • development

Steps

  1. full="$first $last"

    join strings

  2. text+=' more'

    add to the end

  3. echo "${text:0:5}"

    the first 5 characters

  4. echo "${text//old/new}"

    replace every old with new

  5. echo "${text,,} ${text^^}"

    lower case and upper case (bash 4)

  6. echo "${#text}"

    its length

  7. trimmed=$(echo "$text" | xargs)

    without spaces at the ends

Commands used

  • xargs
  • printf

Each has a page in lore: lore xargs opens it.