bashsplit

Word splitting bash parameter on whitespace respecting and retaining quotes


Given the bash parameter

foo='ab   "cd" "e f"  x="1 2" '

I wish to produce an array equivalent to

foo_transformed=( ab '"cd"' '"e f"' 'x="1 2"' )

in a portable way, meaning using either bash (v3+) builtins or programs available to most operating systems (Linux, Unix, Cygwin) by default. Simplicity and safety, given (almost) arbitrary input string, are desirable.

You may assume the input does not contain single quotes ' or backslashes \, but may contain an arbitrary number of whitespace characters, both where I wish them to delimit the string and where they do not (when inside double quotes).

If we try:

foo_transformed=( $foo )

then the internal quotes of foo are not respected (for k in "${foo_transformed[@]}"; do echo "- $k"; done):

- ab
- "cd"
- "e
- f"
- x="1
- 2"

If we try:

eval foo_transformed=( $foo )

then the quotes are lost:

- ab
- cd
- e f
- x=1 2

Solution

  • This might be what you want:

    $ cat tst.sh
    #!/usr/bin/env bash
    
    foo='   ab   "cd"
    "e f"  '\'' x="1 2"
       z="a b"7"c
     d"8   $HOME
              `date`  *  $(date)   '
    
    fpat='(^[[:space:]]*)([^[:space:]]+|([^[:space:]"]*"([^"]|"")*"[^[:space:]"]*)+)'
    
    foo_transformed=()
    while [[ "$foo" =~ $fpat ]]; do
        foo_transformed+=( "${BASH_REMATCH[2]}" )
        foo="${foo:${#BASH_REMATCH[0]}}"
    done
    
    declare -p foo_transformed
    

    $ ./tst.sh
    declare -a foo_transformed=([0]="ab" [1]="\"cd\"" [2]="\"e f\"" [3]="'" [4]="x=\"1 2\"" [5]=$'z="a b"7"c\n d"8' [6]="\$HOME" [7]="\`date\`" [8]="*" [9]="\$(date)")
    

    I gave foo some extra values including globbing chars, a single quote, potential variable references, old and new style command injections, newlines inside and outside of quoted strings, and strings containing multiple quoted substrings so it could test the script more fully.

    The use of the fpat regexp above is inspired by GNU awk's FPAT which is used to identify fields in input, see for example how it's used in CSVs at What's the most robust way to efficiently parse CSV using awk?. It matches: