Problem with Bash script: 'declare: not found'
Are you using sh
instead of bash
? sh
(linked to dash
) does not support declare
keyword, nor the syntax
VAR=(list)
for initializing arrays.
I suspect that your "shebang" line (the optional first line of the file) is referencing sh
instead of bash
. It should be
#!/bin/bash
for bash scripts. If the first line of your script is
#!/bin/sh
then that would indicate that a strictly bourne-compatible shell is to be used; in the case of Ubuntu, dash
is used. In many other distributions, this does not cause a problem, because they link /bin/sh
to /bin/bash
; however ubuntu links to /bin/dash
in order to allow system scripts to run more rapidly.
The declare
builtin is one of bash
's many extensions to the Bourne shell script specification; dash
just implements that specification without extensions.
How to reproduce the above error:
I'm using Ubuntu 14.04 64 bit. Put this code in a file:
#!/bin/sh
declare -i FOOBAR=12;
echo $FOOBAR;
Run it like this:
el@apollo:~$ ./06.sh
./test.sh: 2: ./test.sh: declare: not found
To fix it, do this instead:
#!/bin/bash
declare -i FOOBAR=12;
echo $FOOBAR;
Prints:
el@apollo:~$ ./06.sh
12