[SOLVED] Is the use of '[[' appropriate in this case?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Kyowash

[SOLVED] Is the use of '[[' appropriate in this case?

Post by Kyowash »

I've always checked if a number is greater than another using [[, for example:

Code: Select all

#!/bin/bash
read -p "Type a number: " number
if [[ $number -gt 0 ]]
	then
		echo "Positive."
elif [[ $number -lt 0 ]]
	then
		echo "Negative."
else
	echo "Zero."
fi
Of course, this only works with integers. However, I've read in this website it's pointless and ((number > 0)) should be used instead. My question is: is it really that pointless? Because to be honest, I don't think see a difference or advantage in using the ((expression)) statement, and it will be hard to get used to it.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Is the use of '[[' appropriate in this case?

Post by rene »

Kyowash wrote: Thu Mar 29, 2018 11:34 am My question is: is it really that pointless?
Depends on one's frame of reference I'd say. If you're an advanced bash programmer, used to its arithmetic expressions, then I can imagine that "translating" simple generic arithmetic into special purpose test expressions seems pointless. Writing if ((number > 0)) would also feel more natural to, say, a C programmer used to if (number > 0). Conversely, a reason why some might prefer the "test" syntax is that, if you'd use single [ rather than [[, the test variant is in fact POSIX.

If you try doing actual, serious programming in bash you almost without fail run into its limitations; I as such more often than not feel bash is needlessly complex; that 90% of its features could be slashed without impacting more than 10% of users. I.e., if for serious work you need something else anyway than why bother with all the complexity in the first place? Arithmetic expressions would be part of that which could in that view be slashed, meaning that if I were consistent I'd prefer the test syntax.

I am of course not and prefer the arithmetic syntax -- and would generally feel often handicapped without it on the command line, but still. The answer to your specific question I would believe to be "use whatever you prefer". Bash is I feel not to be considered useful for serious programming anyway, so no pressing reason to become autistically attached to any of its features or syntax either. <shrug>.
Kyowash

Re: Is the use of '[[' appropriate in this case?

Post by Kyowash »

rene wrote: Thu Mar 29, 2018 1:54 pm if you'd use single [ rather than [[, the test variant is in fact POSIX.
This is also one of the things I first ran into when learning Bash, but my decision was to use [[ even if it's not portable for two reasons: it prevents word splitting without having to quote variables and it has the regular expression matching operator =~.
rene wrote: Thu Mar 29, 2018 1:54 pm The answer to your specific question I would believe to be "use whatever you prefer".
I'm glad to hear that. The scripts I make are usually just for me. Bash is perfect to glue together different commands in a simple way, but I want to avoid bad practices. Thank you for taking the trouble to answer my question.
Locked

Return to “Scripts & Bash”