Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Fun thing, manual execution of $( ) command substitution



Hello,
I wrote an ini parser and it allows to put $(...) in the content
because of following function. It isn't a rocket science, and doesn't
much parse to resolve $( echo "$( )" ) cases, but I think that
animating plain-text files is something interesting. Maybe someone has
other such tricks up sleeve?

The function will replace any $( ... ) in given buffer, with
stdout-result of command inside the parentheses, and return new buffer
in REPLY. It uses extended glob. Somewhat robust – handles $(echo \)).

function subst_cmds {
    local buffer="$1" nul=$'\0'
    local -a cmds

    cmds=( ${(0)${(S)buffer//(#b)*\$\((?#)([^\\]\))/${match[1]}${match[2]%\)}${nul}}%$nul*}
)

    [[ "${cmds[1]}" = "$buffer" ]] && return 0

    integer size="${#cmds}" i
    local -a outputs

    for (( i = 1; i <= size; ++ i )); do
        outputs[i]="$( ${(z)cmds[i]} )"
    done

    i=0
    REPLY="${(S)buffer//(#b)\$\((?#)([^\\]\))/${outputs[++i]}}"

    return 0
}

-- 
Best regards,
Sebastian Gniazdowski



Messages sorted by: Reverse Date, Date, Thread, Author