Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh.

Is this statement true or false?

share|improve this question
    
See Why didn't back quotes in a shell script help me cd to a directory for an example of why $(...) notation is easier to use than nested back-quotes. –  Jonathan Leffler Apr 29 at 0:52

1 Answer 1

up vote 65 down vote accepted

Citing the Open Group Specification on Shell Command Languages, specifically this section "2.6.3 Command Substitution" this statement regarding backtick's deprecation is actually false. Both forms of the command substitution, backticks (`..cmd..`) or dollar parens ($(..cmd..)) are still supported in so far as the specification goes.

excerpt

Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution shall occur when the command is enclosed as follows:

          $(command)

          or (backquoted version):

          `command`

The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing $() or backquotes) with the standard output of the command, removing sequences of one or more characters at the end of the substitution. Embedded characters before the end of the output shall not be removed; however, they may be treated as field delimiters and eliminated during field splitting, depending on the value of IFS and quoting that is in effect. If the output contains any null bytes, the behavior is unspecified.

Within the backquoted style of command substitution, <backslash> shall retain its literal meaning, except when followed by: '$', '\`', or <backslash>. The search for the matching backquote shall be satisfied by the first unquoted non-escaped backquote; during this search, if a non-escaped backquote is encountered within a shell comment, a here-document, an embedded command substitution of the $(command) form, or a quoted string, undefined results occur. A single-quoted or double-quoted string that begins, but does not end, within the "`...`" sequence produces undefined results.

With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of re-directions which produces unspecified results.

So then why does everyone say that backticks have been deprecated?

Because most of the use cases should be making use of the dollar parens form vs. the backticks. Many of the most reputable sites (including U&L) often state this as well, throughout, so it's sound advice, but it's just that, good advice.

excerpt

`...` is the legacy syntax required by only the very oldest of non-POSIX-compatible bourne-shells. There are several reasons to always prefer the $(...) syntax

excerpt

This is the older Bourne-compatible form of the command substitution. Both the `COMMANDS` and $(COMMANDS) syntaxes are specified by POSIX, but the latter is greatly preferred, though the former is unfortunately still very prevalent in scripts. New-style command substitutions are widely implemented by every modern shell (and then some). The only reason for using backticks is for compatibility with a real Bourne shell (like Heirloom). Backtick command substitutions require special escaping when nested, and examples found in the wild are improperly quoted more often than not. See: Why is $(...) preferred over `...` (backticks)?.

excerpt

Because of these inconsistent behaviors, the backquoted variety of command substitution is not recommended for new applications that nest command substitutions or attempt to embed complex scripts.

NOTE: This third excerpt (above) is likely the source of why many would consider the backticks as being deprecated. It shows several situations where backticks simply won't work, but the newer dollar parens method does:

Additionally, the backquoted syntax has historical restrictions on the contents of the embedded command. While the newer "$()" form can process any kind of valid embedded script, the backquoted form cannot handle some valid scripts that include backquotes.

If you continue reading that section the failures are highlighted showing how they would fail using backticks, but do work using the newer dollar parens notation.

Conclusions

So it's generally preferable that you use dollar parens vs. backticks but you aren't actually using something that's been technically deprecated. After reading all this you should have the take away that you're strongly encouraged to use dollar parens unless you have a pressing need to use backticks.

share|improve this answer
11  
By most of the meanings of deprecated, I (and you) would say that backticks are deprecated. It's mostly a terminology question. –  Stéphane Chazelas Apr 28 at 15:54
3  
@StephaneChazelas - Agreed! I will continue to tell ppl that they're deprecated but backticks have not been officially "deprecated" in the sense that they're going to be actively removed from the code base of any shell in the near future. At least none that I'm aware of. –  slm Apr 28 at 16:00
2  
My interpretation is that backquotes are supported purely because they are too entrenched in existing code to formally deprecate. I have never heard an argument for continuing to use them in new code. –  chepner Apr 28 at 16:08
2  
@chepner - neither have I, but if you come across them there may be a very good reason that someone who understands the implications might have chosen them over dollar parens. But odds are they were likely used b/c the user just didn't understand there's an alternative in dollar parens. –  slm Apr 28 at 16:14
2  
@slm Yeah, I guess you did get it - I got pulled away and didn't notice. There's almost no reason to use these things, except that it can be just a little cleaner when layering subshells so $( cmd `cmd`) is just a little less messy than $( cmd $(cmd)) - it's easier to read. I've just always considered a deprecated feature in software to be one officially marked for removal by upstream - and I've never heard of anything like that. I don't care - have no love for the grave. –  mikeserv Apr 28 at 18:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.