LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-15-2021, 12:27 PM   #1
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Rep: Reputation: 24
Pros/cons of declare variable in bash


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. 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?


Thanks
 
Old 02-15-2021, 12:35 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,686

Rep: Reputation: Disabled
Sort of poor man input validation I guess.

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.

Last edited by shruggy; 02-15-2021 at 02:14 PM.
 
5 members found this post helpful.
Old 02-15-2021, 01:34 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,156
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Code:
declare -i var

var="abc"
echo "$var"
0

var="123"
echo "$var"
123

var=""$var"+abc"
echo "$var"
123

var=""$var"+20"
echo "$var"
143

read -p "Enter number :" num
Enter number :4
echo "$num"
4

read -p "Enter str :" str
Enter str :abc
echo "$str"
abc
 
Old 02-15-2021, 01:49 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,686

Rep: Reputation: Disabled
Code:
bash -c 'declare -i x;read x <<<"HISTSIZE";echo $x'
or
Code:
bash -c 'declare -i x;read x <<<"SHLVL";echo $x'

Last edited by shruggy; 02-15-2021 at 02:06 PM.
 
1 members found this post helpful.
Old 02-15-2021, 02:20 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,837

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
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]$ ]]
 
Old 02-15-2021, 02:24 PM   #6
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by shruggy View Post
Code:
bash -c 'declare -i x;read x <<<"HISTSIZE";echo $x'
or
Code:
bash -c 'declare -i x;read x <<<"SHLVL";echo $x'
What does this mean, Shruggy? what is this code saying?
 
Old 02-15-2021, 02:44 PM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,686

Rep: Reputation: Disabled
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.
 
Old 02-15-2021, 04:45 PM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Arct1c_f0x View Post
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.)
 
  


Reply



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
Gentoo: Pros and Cons AgtSkippy Linux - Distributions 5 07-12-2003 10:15 AM
subnetting: pros and cons, and an explanation please :) andrewlkho Linux - Networking 4 07-02-2003 01:52 PM
PROS and CONS of mandrake 9.1 and Red Hat 9.. kublador Linux - Distributions 4 07-01-2003 01:26 PM
What're the pros and cons of Debian? Ekim Gram Linux - Distributions 20 06-02-2003 10:18 AM
Pros And Cons Of Linux Oliver_Bell Linux - Newbie 37 05-14-2003 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 AM.

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