4 ms·
The only real advantage of bash (or other shells) is the ability to build up to it from the command line; to use it as a REPL then embed the stuff you've just d
by pjc50 4y ago
The only real advantage of bash (or other shells) is the ability to build up to it from the command line; to use it as a REPL then embed the stuff you've just done into a script.
The massive disadvantage of bash is that it uses a legal filename character " " as a delimiter ("$IFS"). That means it's very easy to write scripts which work fine on your test case but blow up in a different context. And the worst case of that is accidentally deleting the user's file system.
So many of the cute little bash examples you'll see, including I think the awk one on this page, will go wrong if you have a space in the wrong place in your filesystem. Or, god help you, a newline. Or a file named "*". Or "--".
- Joker_vD 4y agoQuotes, dot-slash, quotes, double dash and quotes. Quotes, quotes, shellcheck and quotes. Quotes, quotes, quotes, lovely quotes! Just don't write bugs, what's so hard about shell programming?
- EuAndreh 4y agoJust shellcheck is enough.
- ndsipa_pomu 4y agoShellcheck won't catch the missing dashes ('--') at the end of command options, so you could be in trouble if a variable starts with a dash and the command interprets it as an option rather than the filename. It's not particularly obvious, but if people can upload a file and specify its name, then they could compromise a script by choosing a suitably evil name. If you get into a habit of putting '--' at the end of the options and before the filename variable, then you protect against that.
- JdeBP 4y agoOr, put another way: Getting the 10% of the cases that have whitespace or metacharacters in variable values and filenames to work ... takes the other 90% of the time. (-: I habitually quote variable expansions, even when I know that the variables are never going to contain whitespace. Because long experience has taught me that one day they will. Just like I've learned never to assume that ${PATH} is always non-blank and so assume that PATH="${PATH}:/something" won't accidentally add the current directory to my search path. (-:
- SAI_Peregrinus 4y ago/Τĥιs ñåmè įß ą váĺîδ POSIX /--file paτĥ
- rascul 4y agoIt might be nice to be more restrictive about what can be in a filename. I'm sure there would be a lot of different opinions though.
- 9dev 4y agoOr, you know, it would be nice to have a shell that didn’t assume everyone lives in North America and speaks only a single language..? Lots of things written more recently have shown that support for more, eh, inclusive, character encodings can work just fine.
- rascul 4y agoI meant things like whitespace and non printable characters though. I guess I could have been more specific.
- 9dev 4y agoAh, that’s fair. I thought you were referring to the „strange“ letters used in the parent comment.
- SAI_Peregrinus 4y agoAll the characters in my example are printable. Not all of the octets are, of course, but octets aren't characters. And spaces are extremely common in names, e.g. music track and album names contain spaces more often than not (and thus usually end up in file names). If you can't even support using the biggest key on every keyboard in file names you're doing something horribly wrong.
- wolletd 4y agoI guess the URL path in the Nginx log would be URL-encoded and have a %20 instead of a space. And otherwise, it would actually also a problem of that text based log format that uses spaces as field delimiters itself. Anyway: When I can "build up from the command line", then apparently whatever I'm building up can be easily and intuitively done in the command line. So, stuffing it into a bash script is fast and easy and should be easy to understand as well. I just have real-world applications for small scripts that aren't much bigger than those "cute little bash examples". When you work with embedded systems, you do a lot of repeatable stuff via SSH. I once ported an Installer to Python and now I hate it because it has so much unhelpful noise between the actual relevant commands being executed. Yes, Bash has it's issues and requires some discipline, but so do most other languages.
- d0mine 4y ago> A lot of repeatable stuff via SSH It looks like a use-case for fabric https://docs.fabfile.org/en/stable/getting-started.html#addendum-the-fab-command-line-tool https://docs.fabfile.org/en/stable/getting-started.html#adde... It is the best of both worlds: shell pipeline for one-liners, Python — for complex logic. Or if more declarative approach works in you case then something like ansible could help https://docs.ansible.com/ansible/latest/getting_started/index.html https://docs.ansible.com/ansible/latest/getting_started/inde...
- stcroixx 4y agoHm, I write bash scripts routinely and never do it that way. Always develop and run my scripts as files, starting with ‘set -x’ if I need debug info. I keep interactive mode solely for command line work, not development of scripts I plan to from files.
- hereonout2 4y agoI spent my entire career avoiding file paths with spaces in. On the very rare occasions I'm using an external dataset that includes them I'll make a copy with renamed files using underscores. If more junior team members produce files with spaces in their paths I quickly educate them in how problematic it can be. It's pretty rare this is a problem for me or my team, perhaps we're lucky in what we work on though. Thankfully I have never seen a file named "*" or "--".
- munificent 4y agoHaving to completely change the naming scheme you use for human-authored and human-read artifacts entirely to route around shitting, confusing escaping in a programming language is not a convincing selling point for that language. If you have a file whose contents are Shakespeare's "As You Like It", you should be able to name that file "As You Like It" without having to worry that the computer will vomit and die on it. We deserve better tools.
- ndsipa_pomu 4y agoI too often avoid spaces in filenames, but when writing scripts it's far better to ensure that you cope with them properly. The trick is to always quote your variables by default and then run shellcheck on your script so that it can flag up the corner cases where quoting acts to unquote the string. Of course, it'll also flag up a host of other possible issues which are well worth fixing or at least understanding why it's a possible issue. Another common footgun is not including a double dash ('--') at the end of command options so that the following filename won't be interpreted as an option when it begins with a dash.
- vram22 4y ago>Another common footgun is not including a double dash ('--') at the end of command options so that the following filename won't be interpreted as an option when it begins with a dash. Yes. And if you have a file name that begins with a dash, and you want to delete it, you can use this command: $ rm -- -filename where -filename is that file. Here, the -- (double dash) means "end of the command-line options", so any argument after that is treated as a non-option argument, in this case, a filename for rm to delete. This -- (double dash) works with many Unix commands. It was introduced relatively early on in Unix history, after the issue of filenames starting with a dash was found. Although it is more likely that you would want to rename the file, to remove the dash from the name.