The Bash declare Statement

April 21st, 2009 by Mitch Frazier in

Your rating: None Average: 4.2 (17 votes)

Although rarely used, the bash declare statement does have a couple useful options. It can mark a variable as read only and also mark it as being a number only.

To declare a variable as read only, use the following statement:

declare -r varname

Consider the following script:

#!/bin/bash

a=13
declare -r a
echo $a
a=14
echo $a

When run, the second assignment will fail:

$ sh decl.sh
13
decl.sh: line 6: a: readonly variable

To declare that a variable should accept only numeric values (integers), use the following statement:

declare -i varname

Consider the following script:

#!/bin/bash

declare -i a
a=12
echo $a
a=hello
echo $a

When run, the second assignment will assign zero to the variable rather than the string "hello" that appears in the statement:

$ sh decl2.sh
12
0

The declare statment has other options; the -a option can be used to declare a variable as an array, but it's not necessary. All variables can be used as arrays without explicit definition. As a matter of fact, it appears that in a sense, all variables are arrays, and that assignment without a subscript is the same as assigning to "[0]". Consider the following script:

#!/bin/bash

a=12
echo ${a[0]}
b[0]=13
echo $b

When run it produces:

$ sh arr.sh
12
13

For further options, see the bash man page (search for "^SHELL BUILTINS", then search for "declare").

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dlbb's picture

a=13 declare -r a echo

On June 10th, 2009 dlbb (not verified) says:

a=13
declare -r a
echo $a
a=14
echo
I can not execute this command on my pc, anyone help me?

Mitch Frazier's picture

Are you Using Windows?

On June 10th, 2009 Mitch Frazier says:

It won't work if you are.

Perhaps if you leave a few more details...

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

Joyce Johnson's picture

is linux so great??

On July 27th, 2009 Joyce Johnson (not verified) says:

I had the opportunity to use the linux redhat stuff about 5 years ago. It had the same amount of bugs as the windows competitor at the time. I don't understand the big hooplah. Instead of blue-screening, the PC would just shut down.

TMJ's picture

re: bash

On June 9th, 2009 TMJ (not verified) says:

I runned #!/bin/bash

a=13
declare -r a
echo $a
a=14
echo

but it fail , did i miss anything ? i think i did it right with commands..

It's *supposed* to fail, read the paragraph after the script:

> When run, the second assignment will fail:
>
> $ sh decl.sh
> 13
> decl.sh: line 6: a: readonly variable
__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

Anonymous's picture

Is there any difference

On April 29th, 2009 Anonymous (not verified) says:

Is there any difference between using 'declare -r' and 'readonly' to set variables as read only?

Mitch Frazier's picture

No Difference

On April 30th, 2009 Mitch Frazier says:

Although the man page does not explicitly say they are the same, there does not appear to be any difference in the effect that they have or the error message that's produced if you try to change one.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

Omer's picture

Exact same

On June 15th, 2009 Omer (not verified) says:

It doesn't explain it anywhere on the page, but yeah they're the same for all practical purposes.

Natural Stone's picture

thanks for clarity

On June 11th, 2009 Natural Stone (not verified) says:

I wasn't sure about that either thanks for clearing my confusion.

Anonymous's picture

You can do these with

On April 22nd, 2009 Anonymous (not verified) says:

You can do these with typeset too and it's more portable.

xtifr's picture

to what?

On April 22nd, 2009 xtifr says:

More portable to what? "help typeset" says "Obsolete. See `declare'." And there's no mention of "typeset" in Posix. I was under the impression that it was a korn-shell-ism, and ksh makes little or no pretense to being a viable substiture for a standard /bin/sh, so I see no reason to worry about compatibility with it. Dash (another widely used bourne/posix shell, based on BSD's ash) has "readonly" but neither declare nor typeset, so for that particular case, I would think that "readonly" (also supported by ksh, fwiw) would be slightly more portable. Aside from that, the only factor I see for choosing between the two is that "typeset" looks like something that may not be supported for much longer.

Anonymous's picture

I didn't say typeset is a

On April 22nd, 2009 Anonymous (not verified) says:

I didn't say typeset is a POSIX standard, but works with more shells, like you said ksh -- and its variants. Well, bash is the default shell on Linux, but ksh is very popular on other Unix-like systems.

The fact that typeset is obsolete in bash is surprised me. I can't see why is it good to removing typeset from bash.

xtifr's picture

help

On April 21st, 2009 xtifr says:

Actually, typing "help declare" at the prompt may be quicker/easier than hunting through the very-large man page, at least to get started. (Many people don't realize that bash has built-in help for its built-in commands.)

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue