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.

My friend is messing around with Bash scripts and he wanted me to take a look at them (he had another friend do it for him). One of the commands is

if [ "`pidof transmmission-gtk`"=0 ];

what exactly is the significance of the ` (backquote) before the pidof, especially when it's itself wrapped within double quotes?

For reference it's for the disper resolution package. I don't really use bash that much so that's why I ask.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

The backquotes mean command substitution: the result of expanding `pidof transmission-gtk` is the output from running the command pidof transmission-gtk. An alternate way of writing `pidof transmission-gtk` is $(pidof transmission-gtk), and that alternate way is recommended (it has no downsides and it avoids issues when the command inside the backquotes itself contains quotes).

The double quotes around that prevent the result of the command substitution from being split into separate words and interpreted as wildcard patterns. If you're just starting with shell scripting, don't worry about this, just remember to always put double quotes around command substitutions. The same goes for variable substitutions, e.g. always write "$foo", never plain $foo.

This snippet as written doesn't make any sense: the test contains a single word, which will be something like =0 (if there is no running transmission-gtk process) or 1234=0 (if there is a matching process) or 123 456=0 (a single word in shell terminology, even if it happens to contain a space). A single word in a test is always considered true. To test whether a string is equal to another string, write [ "string1" = "string2" ] (there must be a space on each side of the brackets, and on each side of the operator). To test for numerical equality, use the -eq operator, e.g. [ 1 -eq 01 ] is true.

Here, there are two equivalent ways of testing whether there is a running transmission-gtk process:

  • Test whether the output is empty:

    if [ -z "$(pidof transmission-gtk)" ]; then …
    
  • Test whether the return status of the pidof is nonzero, indicating failure. Since the output of the command is not used, redirect it to /dev/null (the great void).

    if ! pidof transmission-gtk >/dev/null; then
    
share|improve this answer
    
What does the -z do exactly? –  Mercfh Sep 28 '11 at 21:11
    
@Sauron -z tests if its argument is empty. See man [ or the bash manual for a (terse) list of available tests. –  Gilles Sep 28 '11 at 21:21
    
@Giles Ah ok, thanks!! –  Mercfh Sep 28 '11 at 21:25

bash(1) man page, EXPANSION section, Command Substitution subsection:

   Command substitution allows the output of a command to replace the com-
   mand name.  There are two forms:

          $(command)
   or
          `command`

   Bash performs the expansion by executing command and replacing the com-
   mand  substitution  with  the  standard output of the command, with any
   trailing newlines deleted.

The quotes are because [ is about as smart as a cinder block, and doesn't like empty strings.

share|improve this answer
    
+1 for smartness of [ –  Residuum Sep 29 '11 at 13:47

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.