Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/
viwik/ 2: viwik/ [[: not found
viwik/ 8: viwik/ [[: not found

How can I fix this?

share|improve this question
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 – Hồ Tuấn Kiệt 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_n Apr 13 '13 at 23:22
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
up vote 8 down vote accepted

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 ./ 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 with contents:

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

this happens:

$ readlink -f $(which sh)
$ sh 2: [[: not found
$ bash 
This is Bash
$ chmod 755
$ ./
This is Bash
share|improve this answer
Thanks :) It fix my problem. I will learn more thing from this :) – Hồ Tuấn Kiệt 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .