LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I feel stupid: declare not found in bash scripting? (https://www.linuxquestions.org/questions/programming-9/i-feel-stupid-declare-not-found-in-bash-scripting-544199/)

Mohtek 04-08-2007 12:50 AM

I feel stupid: declare not found in bash scripting?
 
I was anxious to get my feet wet, and I'm only up to my toes before I'm stuck...this seems very very easy but I'm not sure what I've done wrong. Below is the script and its output. What the heck am I missing?:cry:

______________________________________________________
#!/bin/bash
declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${ PROD[*]}"
_______________________________________________________

products.sh: 6: declare: not found
products.sh: 8: Syntax error: Bad substitution


mohtech

wjevans_7d1@yahoo.co 04-08-2007 01:17 AM

I ran what you posted (but at the command line, not in a script, though that should make no significant difference), and got this:

Code:

-bash: ${ PROD[*]}: bad substitution
In other words, I couldn't reproduce your first problem, the "declare: not found" error. Try the declare command by itself, on the command line.

And I got rid of the "bad substitution" problem when I removed the space which is between the ${ and the PROD on the printf line.

Hope this helps.

blackhole54 04-08-2007 01:26 AM

The previous poster identified your second problem.

As far as your first problem goes ... I am not a bash guru although I have written a number of bash scripts. So far I have found no need for declare statements. I suspect that you might not need it either. But if you do want to use it, the following does work:

Code:

#!/bin/bash

declare -a PROD
PROD[0]="computers"
PROD[1]="HomeAutomation"
printf "${PROD[*]}\n"

EDIT: My original post was based on an older version of bash. When I tried the declare statement you posted I got an error message, but one that was different from yours. I just tried it on a newer version of bash, and your declare statement worked fine. So it might depend on the version of bash you are running. What I posted above runs fine on both versions.

makyo 04-08-2007 06:37 AM

Hi.

As wjevans_7d1 wrote, removing the space when using the array name allows it to run. Here is a sample:
Code:

#!/bin/bash

# @(#) s1      Demonstrate declare and printf.

bash --version
declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${PROD[*]}"
printf "\n"

Producing:
Code:

% ./s1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
computers HomeAutomation

Best wishes ... cheers, makyo

brianhickey18 04-29-2010 06:00 AM

Im having the similar problem as mohtech
It doesnt seem to recognise declare.
I'm having the same problem with let and basic math code like incrementing a variable.
Even creating an simple array gives me an error
example

names=( john thomas )
syntax error: `names=' unexpected

I reckon I must have something disabled somewhere on the system.
Im working on a beta server at work so I wasn't involved in the initial installation/setup.

any ideas how I could remedy this?

catkin 04-29-2010 06:19 AM

@brianhickey18: please start your own thread and give details of the script before the names= line. Software changes so it's seldom useful to revive a 3+ year old thread.

pixellany 04-29-2010 08:09 AM

And, with that sage advice, old fossil thread now closed....


All times are GMT -5. The time now is 02:44 AM.