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

Forum rules
Before you post please read how to get help
Post Reply
Kyowash
Level 2
Level 2
Posts: 98
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

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

Post by Kyowash » Thu Mar 29, 2018 11:34 am

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 Kyowash on Thu Mar 29, 2018 3:44 pm, edited 1 time in total.

rene
Level 8
Level 8
Posts: 2225
Joined: Sun Mar 27, 2016 6:58 pm

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

Post by rene » Thu Mar 29, 2018 1:54 pm

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
Level 2
Level 2
Posts: 98
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

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

Post by Kyowash » Thu Mar 29, 2018 3:44 pm

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.

Post Reply

Return to “Scripts & Bash”