10 ms·
[ -z $var ] works unreasonably well
- belovedeagle 10y agoBetter lesson? Use zsh, where variables don't need to be quoted because they're expanded as a single shell word.
- yjftsjthsd-h 10y agoThereby destroying compatibility with sh. At that point, why not use Python?
- jamroom 10y agowhile not as portable, using double brackets - i.e. [[ -z $var ]] can be a bit "safer" in regards to quoted variables. Works in bash and KSH. Edit to add: http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts http://stackoverflow.com/questions/669452/is-preferable-over...
- hashhar 10y agoWhy isn't it being added to POSIX? What does it take to get something added to POSIX?
- nine_k 10y agoA time machine?
- stonogo 10y agoThe POSIX standard is under active development.
- hashhar 10y agoPOSIX is under active development. The issue I'm concerned about is this text quoted from the 'printf' man-page. > %q ARGUMENT is printed in a format that can be reused as shell > input, escaping non-printable characters with the proposed POSIX > $'' syntax.
- dozzie 10y agoDon't use that. If anything, use properly quoted variables: [ -z "$var" ] This has literally no possibility of failing in any weird way, no matter which Bourne or Korn shell descendant you use.
- ausjke 10y agoin nearly all cases, you should always do "$var" instead of $var, this should be mandated on sh scripts. you can even go further to do "${bar}", this is even safer I feel.
- scurvy 10y agoYes, it's "${bar}" that should go into style guides and linters (for consistency).
- brazzledazzle 10y agoIt seems kind of petty I guess but that seems kind of annoying to write and even less importantly looks pretty blah.
- dozzie 10y ago"${bar}" is irritating to write. It only makes sense when you need to append a word character ([a-zA-Z0-9_]) to the content of $bar, like in "${bar}qq".
- philipov 10y ago"${bar}" makes my syntax highlighting light up in a satisfying way, and automatic quote/brace completion makes it peachy.
- SubiculumCode 10y agoHave something against [[ ]] ?
- 10y ago
- deleted 10y ago[deleted]
- frou_dh 10y agoFYI the author also offers an absurdly good commandline shellscript linter: https://github.com/koalaman/shellcheck https://github.com/koalaman/shellcheck It's a bit like `checkbashisms` but a lot more expansive and with formal classification of the warnings.
- nodesocket 10y agoshellcheck is awesome. There is even a Sublime plugin SublimLinter-Shellcheck. Also, every rule in shellcheck has a corresponding github wiki page which describes the issues and common fixes. For example SC2002 (https://github.com/koalaman/shellcheck/wiki/SC2002 https://github.com/koalaman/shellcheck/wiki/SC2002)
- jrimbault 10y agoI used it (the linter + its wiki) last week to work on a bunch of related scripts, it was really helpful and even taught me some things about my bad habits.
- philipov 10y agoWoa, it's written in Haskell. Awesome! EDIT: "and requires 2GB of RAM to compile." whaaat?
- jessaustin 10y agoHaha I assumed the second observation was a result of the first? Still, this is why one uses a reasonable distro. When I "apt-get install shellcheck", a version that's just about a year old (0.3.7) gets installed. Let package maintainers worry about compiling Haskell.
- yjftsjthsd-h 10y agoI was about to comment that year-old software is a downside, but given that it's for (ba|)sh maybe not so much; bash changes but glacially, and sh has barely moved in 20 years. Stability for the win :)
- makecheck 10y agoMy rule is that shell scripts with growing complexity must be converted to a language with clearer and more bulletproof treatment of data, like Python. This also happens any time I want to support several command line arguments. At one point in history, options were more limited. Now, it is just not worth the time to read code that may or may not have obscure errors in its very expressions that you must understand before you can even begin to examine the real purpose of the program!
- mbrock 10y agoYou still need to know how to write safe shell programs even if they're one-liners. And once you do, shell is really, really useful and convenient. For example, Linux distributions are full of shell scripts that would be frankly awful to write in any other language that I know of... like this, to take a random example: https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/virtualization/docker/default.nix#L99 https://github.com/NixOS/nixpkgs/blob/master/pkgs/applicatio...
- Jabbles 10y agoShouldn't they be quoted?
- tedmiston 10y agoMost app developers aren't writing the scripts in Linux distros though.
- deleted 10y ago[deleted]
- masklinn 10y agoEh. Looks OK to me, even with Python probably not being the tersest shell language: https://gist.github.com/anonymous/4203b7c6ffc9cb9ecf34d17cb88ecb5e https://gist.github.com/anonymous/4203b7c6ffc9cb9ecf34d17cb8...
- mbrock 10y ago
- AndyKelley 10y ago> The moral of the story? Same as always: quote, quote quote. Even when things appear to work. I would like to propose a different moral of the story, which is, as soon as your shell script requires control flow beyond executing a series of commands one after another, switch to a better programming language, such as Python.
- woah 10y agoShell still has a huge advantage over other scripting languages- the syntax to invoke other programs is completely first class and natural. In real programming languages it's always some .os.exec() or whatever. Is python different?
- fnj 10y agoXonsh (a terrific python-based shell) addresses this with great elegance.
- AndyKelley 10y agosubprocess.check_call and subprocess.check_output are far less error-prone than the first class natural syntax of sh, where you have to remember to quote everything or have hidden bugs, likely even security vulnerabilities. And god forbid your coworker has to try to understand your sh code. Clear syntax is better than pretty syntax.
- ams6110 10y agoUse the proper tool for the job, that I agree with. For most sysadmin tasks, that is shell scripts. Yes you have to be aware of the warts, as with any language or framework. I would no more want to do system administration with Python than I would want to write a web application server in bash.
- idobai 10y agoPython maybe better but there are FAR better languages out there...
- waynecochran 10y agoIs anyone like me and just uses Perl as a Bash replacement? If I need to write more than a 2 line script, I just use Perl.
- ryl00 10y agoPerl is the reason my bash knowledge never grew beyond a rudimentary level...
- dozzie 10y agoI used to write many Perl tools for command line (I currently use Python because of team's tribal knowledge), though I make the choice between Perl/Python and shell looking not at the expected size, but at what kind of processing it is. Shell is an excellent tool when it comes to call external commands and generally working with files, and no other language makes it as easy.
- _delirium 10y agoReplacing bash+sed+awk is more or less why Perl was written initially (which also explains much of its syntax). I like it better than shell scripts mostly because the built-in data structures are a bit nicer. And I like Perl/PCRE regex syntax better than POSIX regex syntax.
- manyxcxi 10y agoI went the opposite way. I started out writing few liner bash scripts that just basically wrapped my perl script with proper environment vars, args, etc. and do all the heavy lifting in Perl. It also happens that my dad was a a build engineer back when they didn't really have a title for it and his Swiss Army knife was Perl. I literally learned coding beyond BASIC on HP-UX, Solaris, and Perl. At some point, I just started doing more and more in bash and started using Perl only specifically for anything I was having trouble getting working with sed+awk. As of today it's probably been 3+ years since I wrote anything in Perl, I'll just drop to Python if I need it now. Also my sed and awk skills have gotten better.
- tedmiston 10y agoBash is such a painful and error-prone language, I'm surprised anyone still writes it accept as a last resort. I've tried to mostly replace bash scripts with Python personally. It'd be excellent to see a bash meta language emerge something like what CoffeeScript or ES6 have done for JavaScript. App-level programmers shouldn't have to deal with this kind of bad "UX" regularly.
- BrandonBradley 10y agoHas replacing scripts with Python worked well for you? Context: I have the same feelings about Bash.
- tedmiston 10y agoWell enough with libraries like Fabric and Click. Though my projects still occasionally have Makefiles with bash one-liners for commands like what one would put in package.json in a Node app. It's almost all upside. The only real catch is if you're running against an unknown system Python version, but using virtual environments solves that and is the best practice anyway even if it feels a little heavy for one file < 100 lines. I also really like the pattern Django uses for extensible custom commands. I haven't built a system that extensive for my own apps yet but have enjoyed working with it when I am doing Django apps. https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/ https://docs.djangoproject.com/en/1.11/howto/custom-manageme...
- phjesusthatguy3 10y agoPeople continue to write in English as well, and except the errors within it :)
- yes_or_gnome 10y agoThis problem isn't a Bash problem, nor is it a shell problem. '[' is a binary that takes a set of args terminates with a ']'. It's often just a symlink to the 'test' binary. The other "problem" is quoting. In which case what would you expect to happen? Most people avoid spaces in file paths for this very issue. Argument passing is whitespace separated, so an application has no idea that you meant two (or more) args to be just one. Simple solution, use quotes. If you were to shun all CLI usage in favor of another programming language, then you would be doing the exact same thing with system calls; Using the example from the article for consistency (even though it wouldn't make much sense in a more expressive language): var = "some value"; exec("[", "-z", var, "]");
- munchhausen 10y agoPlease. Use `[[ -z $var ]]` instead, and get this load of nonsense off your table. And yes, `[[ ... ]]` is a bashism. It's not going work on the HP-UX box, that you do not have in your server fleet.
- fnj 10y agoIt won't work on dash or on BSD ash either. Do what you want, but you won't be doing it working for me because I care about portability.
- pyre 10y agoBetter make sure those shell scripts all run in csh/tcsh too. Never know when you'll be running on MacOS 10.1!
- stonogo 10y agocsh is still the default root shell on FreeBSD, which is in heavy use in millions of places around the world. There are also a relatively large number of tcsh users in the world. This is why, even now on latest fedora, /etc/profile.d contains csh scripts. As for sh, standards exist for a reason. Try to empathize with people who are not in your bubble.
- shakna 10y agoDash is on all my main servers, but Busybox is on the vendored stuff. That means dash & ash. Portability can matter.
- mort96 10y agothe nice thing about standards is that you don't have to make sure the script runs in a billion different shells. You just have to make sure your code is POSIX compatible, and all of a sudden, all POSIX compatible shells will interpret it just fine! Isn't that fantastic?
- comex 10y ago
- jstanley 10y agoWhat about if $var is '== -z' :)
- raphlinus 10y agoConfirmed. The title statement is wrong. I think the moral of _that_ story is that not even renowned experts can reliably get bash right.
- gwu78 10y agoCan an "empty" variable have a length greater than zero? In Bourne once scripts I have been testing test ${#var} -gt 0 and test ${#var} -ge 1 In the past I have seen others use test x"" = x$var
- feld 10y agoYes, the test to see if "x" matches is the old school trick I was taught
- IshKebab 10y agoYeah, in that it vaguely works in some cases.
- cryptarch 10y agoHow is: [ -z $var ] different from: [ $var ] ? I don't understand the distinction between a length-0 string and "the null string" in the context of /bin/sh. I always just use [ "$var" ] and it's never failed me so far; should I be worried?
- LambdaComplex 10y agoInteresting, but not all that surprising when you stop to think about it: give it invalid arguments and it fails, with a corresponding exit code. As an aside/self-plug, I wrote a blog post[0] complaining about my least-favorite Bash gotchas. 0. https://lambda.complex.rocks/2017/03/why-i-dont-like-bash/ https://lambda.complex.rocks/2017/03/why-i-dont-like-bash/