Safe ways to do things in bash
161–170 of 255 posts
Re: Safe ways to do things in bash
#162If you reached a point where you need to require bash and not a posix shell and need to enforce these rules just use python or lua if you can or some scheme or whatever else... it's not worth the wasted time hunting bash cruft. if you are on busybox with ash none of this is helping (except shellcheck which is great).
Bash is available on almost every system. The portability is desirable sometimes.
Re: Safe ways to do things in bash
#163Backticks are very error prone $(cat foo.txt) is much more explicit and visually clearer for command substitution.
Re: Safe ways to do things in bash
#164Earlier quoted context omitted.
I have made a lot of DNS products and doing the same thing happening here is a few lines of code in most any language. This code, while entertaining, cannot handle very simple DNS packets because of its "compression encoding.
This is ran behind PowerDNS, which handles the compression prior to handing off to this code, so that shouldn't be a problem.
Re: Safe ways to do things in bash
#165Step 1 to write safer shell scripts: use a safer shell. Zsh gives the user much more control, has safer defaults, and is itself quite portable (even if Zsh scripts are not portable to other shells).
unfortunately, zsh is installed on probably about 1% of Linux systems worldwide. maybe it can be installed almost anywhere, but the fact is it isn't, and you might as well use Python or something at that point.
Re: Safe ways to do things in bash
#166A tip from me, based on a mistake I made yesterday: don't `source ~/.bash_history` instead of `~/.bash_profile`. (Luckily it entered vim relatively soon, from where I could kill the process).
Re: Safe ways to do things in bash
#167Earlier quoted context omitted.
I did a talk on this a while ago: https://www.youtube.com/watch?v=pb3k0sGKrjQ&t=457s 'Take bash seriously'
It's time we need a better shell than bash instead of thinking it's great after bleeding with it for years and gets used to it.
Re: Safe ways to do things in bash
#168> POSIX mandates /bin/sh Nope. On the contrary, it says: Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh Source: http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.... A pedantically-compliant shell script should not have shebang at all.
Re: Safe ways to do things in bash
#169> POSIX mandates /bin/sh Nope. On the contrary, it says: Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh Source: http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.... A pedantically-compliant shell script should not have shebang at all.
What's wrong with `#!/usr/bin/env sh`?
Re: Safe ways to do things in bash
#170I've written a ridiculous amount of shell script in my day, especially when doing "devops" before we had a term like "devops" to describe it. I've fallen in almost every pit bash has. With that background, here is my opinion. 1. This article contains excellent advice and should be starred for later retrieval. 2. Having basic scripting skills will make you a way better programmer. Many times I've done huge refactors a…
I think it's time people start using something better than bash/zsh that is decades old, like fish or even come up with a more modern shell. Even by looking at these examples, you see it has less verbosity like "then" and "do", you can reference arguments as $argv instead of cryptic $@ and exit status code as $status instead of $? which is confusing with $! and the likes. https://blog.codeship.com/lets-talk-about-she…