variable substitution

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.

2

1 Answer

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like