Results 1 to 6 of 6

Thread: Difference between 'test' and '[[ ]]'

  1. #1
    Join Date
    Nov 2005
    Location
    metro Philadelphia, PA, U
    Beans
    53
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Difference between 'test' and '[[ ]]'

    When I run the following on a Terminal command line, it works as I expect—

    Code:
    ~$ if [[ 0 -eq 0 ]]; then touch /tmp/commandline-if; fi
    ~$ ls -alF /tmp/commandline-if
    -rw-rw-r-- 1 hugh hugh 0 Dec 30 17:43 /tmp/commandline-if
    When I add the following as a one-time cron job via the Scheduled tasks application, it does not work as I expect—

    Code:
    if [[ 0 -eq 0 ]]; then touch /tmp/cron-if; fi
    This does not result in the specified file being created—

    Code:
    ~$ ls -alF /tmp/cron-if
    ls: cannot access /tmp/cron-if: No such file or directory
    But if I change all occurrences of '[[ ]]' to a test command, everything works as expected. Adding

    Code:
    if test 0 -eq 0; then touch /tmp/cron-if; fi
    as a one-time cron job does indeed create the specified file—

    Code:
    ll /tmp/cron-if
    -rw-rw-r-- 1 hugh hugh 0 Dec 30 20:31 /tmp/cron-if
    Is this a bug? Or am I missing something?
    Linux—the choice of a GNU generation

  2. #2
    Join Date
    Apr 2012
    Beans
    6,179

    Re: Difference between 'test' and '[[ ]]'

    maybe because the [[ ... ]] extended test is a bashism, and cron jobs are run in /bin/sh which is symlinked to the dash shell by default?

    The single bracket form [ 0 -eq 0 ] should work I think - in fact since you are using the -eq integer test operator it would be more natural, imho (people tend to use ==, !=, < > operators with [[ ... ]] since it supports them)

    http://mywiki.wooledge.org/Bashism

  3. #3
    Join Date
    Nov 2005
    Location
    metro Philadelphia, PA, U
    Beans
    53
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Difference between 'test' and '[[ ]]'

    Quote Originally Posted by steeldriver View Post
    maybe because the [[ ... ]] extended test is a bashism, and cron jobs are run in /bin/sh which is symlinked to the dash shell by default?
    Ah, good point! I didn't think about that.

    The single bracket form [ 0 -eq 0 ] should work I think - in fact since you are using the -eq integer test operator it would be more natural, imho (people tend to use ==, !=, < > operators with [[ ... ]] since it supports them)
    '[ ... ]' does work. Also '==' works as a test for integer equality with both bash and sh. I guess I've always used '-eq' (and similar variants) because that's what 'help test' shows as the way to do it.

    Good: I'll use this to make my scripts more POSIX compliant.

    Thanks for answering my question and clearing up my confusion!
    Linux—the choice of a GNU generation

  4. #4
    Join Date
    Dec 2012
    Beans
    9

    Re: Difference between 'test' and '[[ ]]'

    Quote Originally Posted by steeldriver View Post
    maybe because the [[ ... ]] extended test is a bashism, and cron jobs are run in /bin/sh which is symlinked to the dash shell by default?

    The single bracket form [ 0 -eq 0 ] should work I think - in fact since you are using the -eq integer test operator it would be more natural, imho (people tend to use ==, !=, < > operators with [[ ... ]] since it supports them)

    http://mywiki.wooledge.org/Bashism
    I disagree:
    -eq is used to compare number
    == and = is used to compare strings!
    Last edited by HomelandSecurity; December 31st, 2012 at 04:15 AM.

  5. #5
    Join Date
    Dec 2012
    Beans
    9

    Re: Difference between 'test' and '[[ ]]'

    there is a huge dif between [] and [[]]

    http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

    try to change the first part of the comparison with a variable (with an integer value added to it). and repeat the condition.
    Last edited by HomelandSecurity; December 31st, 2012 at 04:23 AM.

  6. #6
    Join Date
    Mar 2011
    Location
    Goshen, IN
    Beans
    396
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Difference between 'test' and '[[ ]]'

    if it works in bash, then just create a bash script that gets called by cron.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •