0

I found this kind of script here but it's not working , why ?

while true
do
    echo "which number ?"
    read number
    if [[ $number =~ ^[A-Za-z_]+$ ]]; then
        echo "no letters!"
   else
        break
    fi
done
echo "good!"

I get the following :

./A.sh: 7: ./A.sh: [[: not found

thanks for help !

SoFa7
  • 19
  • 2
    The first line of the script is probably `#!/bin/sh`, but `[[` is Bash feature. Change the first line to `#!/usr/bin/env bash` – janos Feb 11 '17 at 16:04
  • @janos The shebang shouldn't be used to try to enforce some sense of portability. The script might be written for a specific version (or at least specific minimum version) of `bash`, and you have no idea if the users's `PATH` will result in the correct version of `bash` being used. Shebangs should be considered a *local* convenience only, unless you are targeting a published standard like `/bin/sh`. – chepner Feb 11 '17 at 16:12
  • @chepner I'm not sure if I got you correctly. Can you elaborate on that *local convenience* part? – hek2mgl Feb 11 '17 at 16:28
  • The shebang only specifies a *path*; you as the script writer have no way of knowing what is actually installed at that location unless it is a machine under your control. As a real example, `/bin/bash` on a Linux machine is typically much different (read: newer) than `/bin/bash` on a Mac OS X. However, the POSIX standard specifies very specifically what `/bin/sh` needs to refer to, so you could safely use that as a portable shebang. Otherwise, the shebang is nothing more than a rough hint as to what the script should be executed with. – chepner Feb 11 '17 at 16:34
  • Python's distribution tools take an interesting approach. The script writer simply uses `#!python` as a placeholder. The installation process uses the *installer's* local configuration to replace that with a path to a Python interpreter on the target machine. – chepner Feb 11 '17 at 16:38
  • Thanks for the explanation! You are right, and for portability reasons I would not use bash for anything that does not run on my machine only. Anyhow, using `#!/bin/bash` will likely solve the OP's problem. – hek2mgl Feb 11 '17 at 17:08

0 Answers0