cm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agoLike programming in bashlemmy.mlimagemessage-square179fedilinkarrow-up11.56Karrow-down116
arrow-up11.55Karrow-down1imageLike programming in bashlemmy.mlcm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agomessage-square179fedilink
minus-squareClemaX@lemm.eelinkfedilinkarrow-up12·edit-21 day agoIt all makes sense when you think about the way it will be parsed. I prefer to use newlines instead of semicolons to show the blocks more clearly. for file in *.txt do cat "$file" done The do and done serve as the loop block delimiters. Such as { and } in many other languages. The shell parser couldn’t know where stuff starts/ends. Edit: I agree that the then/fi, do/done case/esac are very inconsistent. Also to fail early and raise errors on uninitialized variables, I recommend to add this to the beginning of your bash scripts: set -euo pipefail Or only this for regular sh scripts: set -eu -e: Exit on error -u: Error on access to undefined variable -o pipefail: Abort pipeline early if any part of it fails. There is also -x that can be very useful for debugging as it shows a trace of every command and result as it is executed.
minus-squarestetech@lemmy.worldlinkfedilinkarrow-up1arrow-down1·edit-21 day ago set -euo pipefail Fun fact, if you’re forced to write against POSIX shell, you aren’t allowed to use these options, since they’re not a thing, which is (part of) the reason why for example Google doesn’t allow any shell language but bash, lol.
minus-squarejxk@sh.itjust.workslinkfedilinkarrow-up4·21 hours agoBtw, all three set options given above are included in POSIX since 2024: https://pubs.opengroup.org/onlinepubs/9799919799/
minus-squarestetech@lemmy.worldlinkfedilinkarrow-up2arrow-down1·21 hours agoOoh, you’re totally right!! I forgot about that since it’s not in the older versions.
It all makes sense when you think about the way it will be parsed. I prefer to use newlines instead of semicolons to show the blocks more clearly.
for file in *.txt do cat "$file" done
The
do
anddone
serve as the loop block delimiters. Such as{
and}
in many other languages. The shell parser couldn’t know where stuff starts/ends.Edit: I agree that the
then
/fi
,do
/done
case
/esac
are very inconsistent.Also to fail early and raise errors on uninitialized variables, I recommend to add this to the beginning of your bash scripts:
set -euo pipefail
Or only this for regular sh scripts:
set -eu
-e
: Exit on error-u
: Error on access to undefined variable-o pipefail
: Abort pipeline early if any part of it fails.There is also
-x
that can be very useful for debugging as it shows a trace of every command and result as it is executed.Fun fact, if you’re forced to write against POSIX shell, you aren’t allowed to use these options, since they’re not a thing, which is (part of) the reason why for example Google doesn’t allow any shell language but bash, lol.
Btw, all three set options given above are included in POSIX since 2024: https://pubs.opengroup.org/onlinepubs/9799919799/
Ooh, you’re totally right!! I forgot about that since it’s not in the older versions.