This is my code:

if [[ -d ~/viwiki ]]; then
 cd ~/viwiki
 mkdir ~/viwiki
 cd ~/viwiki
if ! [[ -d ./log ]]; then
 mkdir log
 mkdir log/log
 mkdir "log/wget"
elif ! [[ -d ./log/log ]]; then
 mkdir log/log
elif ! [[ -d "./log/wget" ]]; then
 mkdir "log/wget"

When running, it has errors:

tuankiet65@UbuntuPC:~$ sh viwik/test2.sh
viwik/test2.sh: 2: viwik/test2.sh: [[: not found
viwik/test2.sh: 8: viwik/test2.sh: [[: not found

How can I fix this?

  • 4
    Maybe the Bash version is too old? – Daniel Beck Feb 14 '13 at 10:56
  • I don't think so because I am running Ubuntu 12.10 – Kiet Ho Feb 14 '13 at 11:03
  • [[ is ancient, added bash-2.02, released April 1998. – mr.spuratic Feb 14 '13 at 11:54
  • not even using bash; using sh. run scripts using ./{script} (after chmod u+x), not sh {script} – michael Apr 13 '13 at 23:22
  • 1
    Old question, I know, but the entire code above could be replaced by the following mkdir command: mkdir -p ~/viwiki/log/{log,wget} – Dennis Mar 7 '14 at 2:15

I'm guessing that if you run

readlink -f $(which sh)

you will not get Bash as return value, but Dash. You have the correct preamble, but that only matters if you run the script as ./test2.sh after making it executable.

Right now you force-run the script via the sh interpreter, which probably is Dash, and the [[]] construct is a Bash specific one.

That's "why?". If you just replace the double brackets with single ones (and change #!/bin/bash to #!/bin/sh, since your script is now only using POSIX functions anyway) it should run as intended.

Demonstration on Debian, with test.sh with contents:

if [[ "string" == "string" ]]; then
    echo This is Bash

this happens:

$ readlink -f $(which sh)
$ sh test.sh 
test.sh: 2: test.sh: [[: not found
$ bash test.sh 
This is Bash
$ chmod 755 test.sh
$ ./test.sh
This is Bash
| improve this answer | |
  • Thanks :) It fix my problem. I will learn more thing from this :) – Kiet Ho Feb 14 '13 at 11:15
  • @HồTuấnKiệt: The Ubuntu wiki is a good resource on differences between Dash and Bash, with rationale on why the change was made. – Daniel Andersson Feb 14 '13 at 11:21

Your Answer

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

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