Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Closed Thread
  Search this Thread
Old 04-08-2007, 01:50 AM   #1
Registered: Dec 2004
Location: Denver Colorado
Distribution: Kubuntu/Debian
Posts: 122

Rep: Reputation: 15
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?

declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${ PROD[*]}"
_______________________________________________________ 6: declare: not found 8: Syntax error: Bad substitution

Old 04-08-2007, 02:17 AM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

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

-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.
Old 04-08-2007, 02:26 AM   #3
Senior Member
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
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:


declare -a PROD
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.

Last edited by blackhole54; 04-08-2007 at 02:35 AM.
Old 04-08-2007, 07:37 AM   #4
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 731

Rep: Reputation: 75

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

# @(#) s1       Demonstrate declare and printf.

bash --version
declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${PROD[*]}"
printf "\n"
% ./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

Last edited by makyo; 04-08-2007 at 08:04 AM.
Old 04-29-2010, 07:00 AM   #5
LQ Newbie
Registered: Apr 2010
Posts: 3

Rep: Reputation: 0

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

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?
Old 04-29-2010, 07:19 AM   #6
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
@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.
Old 04-29-2010, 09:09 AM   #7
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
And, with that sage advice, old fossil thread now closed....

Closed Thread

bash, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid Scripting Question steve_f60 Linux - General 2 03-10-2006 04:21 PM
God do I feel stupid, please read, laugh and help! jmpmjmpm Linux - General 12 02-22-2005 05:18 PM
question about bash look and feel Hockeyfan SUSE / openSUSE 4 12-28-2004 05:29 PM
I feel stupid asking this... mjakob Linux - General 6 01-21-2002 04:31 AM
I Feel Stupid:) Insanepyro69 Linux - Newbie 21 11-11-2001 01:19 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration