I found this on shell script, in variable substitution option using cap symbols can anyone explain the logic of this nf=${f:gs^__^/^}

for f in  notes__* books__*; do
    nf=${f:gs^__^/^}
    perl -p -e $f > /home/bob/$nf
  done

Any help would be great, thanks.


You tagged your question both bash and zsh, however AFAIK the :s syntax is only supported in zsh (borrowed from csh). The bash equivalent would be ${f//__/\/} - which is also supported in zsh (and borrowed from ksh).

:gs introduces a global substitution inside the parameter expansion. ^ is an arbitrary (user-supplied) delimiter - the usual choice would be / but in this case it appears the replacement text is / so ^ is chosen instead. So given

f=notes__foo__bar

${f:gs^__^/^} globally replaces __ with /:

 % echo ${f:gs^__^/^}
notes/foo/bar

Refer to man zshexpn for further details.