Date: Sun, 20 Feb 2011 19:12:46 +0100 From: David Demelier <demelier.david@gmail.com> To: freebsd-questions@freebsd.org Subject: Re: Backtick versus $() Message-ID: <4D61599E.4040008@gmail.com> In-Reply-To: <alpine.BSF.2.00.1102201027170.56885@wonkity.com> References: <alpine.BSF.2.00.1102201027170.56885@wonkity.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 20/02/2011 18:40, Warren Block wrote:
> $() apparently isn't quite the same as backticks, although sh(1) doesn't
> mention that, or I just missed it. This script is just supposed to
> escape special characters* in a path/filename:
>
> #!/bin/sh
>
> DESTDIR="./"
> COMPFILE=".cshrc"
>
> PSTR=`echo "${DESTDIR}${COMPFILE}" | sed 's%\([?:.%\\]\)%\\\1%g'`
> echo ${PSTR}
>
> PSTR=$(echo "${DESTDIR}${COMPFILE}" | sed 's%\([?:.%\\]\)%\\\1%g')
>
> % ./test.sh
> \1/\1cshrc
> \./\.cshrc
>
> With backticks, the backreference \1 never seems to be replaced with the
> actual pattern, regardless of search pattern. Tested on 8-stable and
> 9-current.
>
> *: That's special characters as less(1) -Ps sees them.
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to
> "freebsd-questions-unsubscribe@freebsd.org"
I'd prefere $() rather than ``. It's more powerful, for example you can
write a multiple $() but not `` see :
markand@Abricot ~ $ echo $(basename $(which dmesg))
dmesg
markand@Abricot ~ $ echo `basename `which dmesg``
usage: basename string [suffix]
basename [-a] [-s suffix] string [...]
which dmesg
Of course the example code is useless but shows the limitations of ``.
Nowadays all shells supports $() so I advise you to use it :).
Cheers,
--
David Demelier
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4D61599E.4040008>
