What is a best practice to represent a boolean value in a shell script?
21
votes
5
answers
53766
views
I know, that there are boolean values in
bash
, but I don't ever see them used anywhere.
I want to write a wrapper for some often looked up information on my machine, for example, is this particular USB drive inserted/mounted.
What would be the best practice to achieve that?
* A string?
drive_xyz_available=true
* A number (0 for true, ≠0 for false)?
drive_xyz_available=0 # evaluates to true
* A function?
drive_xyz_available() {
if available_magic; then
return 0
else
return 1
fi
}
I mostly wonder about, what would be expected by other people who would want to use the wrapper. Would they expect a boolean value, a command like variable or a function to call?
From a security standpoint I would think the second option is the safest, but I would love to hear your experiences.
Asked by Minix
(6065 rep)
Feb 19, 2015, 09:57 AM
Last activity: Aug 8, 2025, 12:39 PM
Last activity: Aug 8, 2025, 12:39 PM