Bash function returns 44 for a 300 GB folder because return sets exit status, not data
A small bash function meant to report a folder's size in gigabytes returned a wrong value on a production server, showing 44 GB for a folder holding 300 GB. The problem stems from using return, which only sets an exit status limited to numbers from 0 to 255, so larger values wrap around. The same mistake exposes a related issue where variables set inside a function without the local keyword overwrite same-named variables elsewhere in the script. The suggested fix is to have the function print its result and capture that output with command substitution, while sending status messages to standard error. The article also covers function arguments, conditional checks on status, and helper functions for administrative scripts.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in