Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 7 down vote accepted

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

VAR=(list) 

for initializing arrays.

share|improve this answer
    
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 '11 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 '11 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 '11 at 16:42
    
@Ashfame: I don't know, this script, as you show it now, cannot work with sh script.sh. –  enzotib Feb 27 '11 at 16:49
    
ok may be I am wrong. Thanks! :) –  Ashfame Feb 27 '11 at 16:54

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.

share|improve this answer
    
My shebang line is #!/bin/bash only –  Ashfame Feb 27 '11 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 '11 at 16:57
    
@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 '11 at 18:03
    
@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 '11 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 '11 at 20:09

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
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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