ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#! /usr/bin/bash
declare -i H
declare -i M
echo -e "Set alarm hour value"
read H
echo -e "Set alarm minute value"
read M
echo "$H:$M"
What advantages are there to using the declare command to declare shell variables as opposed to just setting the variables with read. Note that in both cases the observable effect is the same. Is there something that I'm missing? Something that is going on behind the scenes? What advantages are there to either using the declare command or just initializing the variables with the read command?
declare -i declares an integer variable. When such variable is assigned a value, arithmetic evaluation is performed.
According to the rules of arithmetic evaluation, if you assign a non-number value to such a variable, and the assigned string can be construed as a variable name, it will be evaluated as if it were the name of another shell variable. If that variable is unset or null, it will evaluate to 0. If assigned value is syntactically wrong for a variable name or for an arithmetic expression, you'll get a syntax error.
I do not use declare -i
It might give unwanted results on wrong input (cast to 0).
I prefer to accept all input then validate like [[ $var =~ ^[0-9]$ ]]
Just two examples of input that could be already defined variables in Bash with numeric values. I mean, try to enter SHLVL in your script instead of hour or minute value.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818
Rep:
Quote:
Originally Posted by Arct1c_f0x
Consider the following script:
Code:
#! /usr/bin/bash
declare -i H
declare -i M
echo -e "Set alarm hour value"
read H
echo -e "Set alarm minute value"
read M
echo "$H:$M"
What advantages are there to using the declare command to declare shell variables as opposed to just setting the variables with read.
Good question. I used to declare arrays but then I found that:
Code:
$ A=( $( cat random.txt ) )
worked the same when "A" wasn't declared as it did when it was.
I can't say whether that is true of all possible variations of "define", though. One advantage I can see is that a variables planned usage is right there for the reader to see at the top of the script. (I would never want to sprinkle "define" statements all around in a script. That would lead to madness.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.