27

I had a script which was running fine but when I ran it today, it says declare: not found. I am using bash shell and path at the starting of the script is correct.

Two flagged lines in my script are as follows:

declare -a RESPONSE
RESPONSE=($RESULT)

It also says ( is unexpected but I guess that is coming up because of the first error. Worth mentioning point is when I type in declare directly works fine.

declare | grep USER shows

USER=ashfame
USERNAME=ashfame
           values="$SVN_BASH_USERNAME";

So, whats wrong here?

6 Answers 6

32

Are you using sh instead of bash? sh (linked to dash) does not support declare keyword, nor the syntax

VAR=(list) 

for initializing arrays.

4
  • My shebang line is #!/bin/bash only. Did you mean how I execute it? I just tried executing it as ./script.sh and it worked. But it doesn't work as script.sh or sh script.sh and I am pretty much sure I used to use only the latter two methods while I created it a week ago. What could explain that?
    – Ashfame
    Feb 27, 2011 at 16:22
  • @Ashfame: maybe a week ago it missed the features specific to bash you have now, so it worked also with sh script.sh
    – enzotib
    Feb 27, 2011 at 16:40
  • No I meant exactly this script used to work a week or two weeks ago. Or you meant some update might have made a change?
    – Ashfame
    Feb 27, 2011 at 16:42
  • @Ashfame: I don't know, this script, as you show it now, cannot work with sh script.sh.
    – enzotib
    Feb 27, 2011 at 16:49
11

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.

8
  • My shebang line is #!/bin/bash only
    – Ashfame
    Feb 27, 2011 at 16:06
  • @Ashfame: how are you running the script? If you are invoking it by just typing the name of the script, the shebang line will determine what interpreter runs it. However, if you explicitly invoke it via sh thescript.sh, the sh shell — i.e. dash will interpret it.
    – intuited
    Feb 27, 2011 at 16:57
  • 1
    @intuited I was running it through sh script.sh. I understand what you said, but I have read somewhere that with this method, bash will interpret it. Why would dash get it role here? Is it with Ubuntu only? May be what I read was for linux in general.
    – Ashfame
    Feb 27, 2011 at 18:03
  • 2
    @Ashfame with sh script you are running the command sh with the argument script, which makes sh read and execute the commands in that file. The shebang is not read by sh, it starts with a #, so it's treated as a comment. If you instead run ./script, then the kernel will read the shebang, which in your case is #!/bin/bash, and so it executes /bin/bash ./script
    – geirha
    Feb 27, 2011 at 18:48
  • @geirha yes thats how I know it works but @intuited mention that dash will be used, so I need to clarify it
    – Ashfame
    Feb 27, 2011 at 20:09
3

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
1

I had the same issue, and then I remembered that you need to assign the right permissions to execute the shell script.

Change permission of the script, for example:

chmod 755 script.sh
1

Try running your script using /bin/bash in command line instead if sh (:

Instead of :

    sh script.sh

Try :

    /bin/bash script.sh
0

On Ubuntu, you may have a script which is executing other scripts within it, which I have run into.

some_dir/  
        | main.sh  
        | shhh.sh

#!/bin/bash -e

echo "file? $0"
# file? ./main.sh

echo "shell? $SHELL" 
# shell? /bin/bash

# sh shhh.sh
# file? shhh.sh
# shell? /bin/bash
# shhh.sh: 5: shhh.sh: declare: not found

$SHELL shhh.sh
# file? shhh.sh
# shell? /bin/bash
# foo? bar

#!/bin/bash -e

echo "file? $0"
# shhh.sh

echo "shell? $SHELL"
# shell? /bin/bash

declare foo="bar"

echo "foo? $foo"

Changing all sh some_script.sh to $SHELL some_script.sh fixed it.

You must log in to answer this question.

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