Hacker News new | past | comments | ask | show | jobs | submit login
Bash [ -z hello ] (duckduckgo.com)
174 points by ca98am79 on June 25, 2014 | hide | past | favorite | 61 comments



And yet, the top search result is tldp. This is what #bash on freenode has to say about that site:

> The infamous "Advanced" Bash Scripting Guide should be avoided unless you know how to filter out the junk. It will teach you to write bugs, not scripts. In that light, the BashGuide was written: http://mywiki.wooledge.org/BashGuide

Now, that site is on the second "page" of the search results.


Do they have examples of how it's bad as opposed to simply declaring it bad?


Well for example, I just skimmed through it and I don't see an explanation of quoting, probably one of the first things I would want to explain, around the time variables are explained. It also has examples like this:

        #!/bin/bash
        for i in $( ls ); do
            echo item: $i
        done
        
which both uselessly calls ls (you may as well just use a glob) and doesn't work as intended if there are filenames with spaces in the directory.

You also have:

        #!/bin/bash
        for i in `seq 1 10`;
        do
                echo $i
        done    
        
Why is it using backticks here instead of $() as in thr former example? Also, no mention that seq is not available on some systems. And, you should be using bash's sequence expression[1] anyway (added in bash 4.x), so the guide is likely out of date.

1. http://www.gnu.org/software/bash/manual/bashref.html#Brace-E...


Using ls instead of a glob can be valid if you need to force ordering for some reason (you want to process things in date order for instance, IIRC the order out of a glob wildcard is alphanumeric in most places and arbitrary in some). Your point about spaces in filenames is definitely valid though but my preferred solution to that would be to stab people with a needle every time they put a space in a filename!


Bah, spaces are perfectly cromulent characters. It's the bourne shell that's broken. rc works fine with them.


There are a great many things on the command line (less elsewhere, but still some) that don't handle spaces in filenames well.

You are probably right that this is a technological fault rather than a problem with spaces themselves, but IMnsHO until we are not regularly using spaces as list item separators using them with the items being listed is asking for issues.


You can easily deal with spaces in bourne shell. Quote your variables. Change the IFS to make it only newlines. Of course, newlines in filenames are perfectly valid too.


Spaces are perfectly okay in a filename. Not that I use them, but this isn't 1990 anymore.


I use some systems without seq, and they have Bash with version 2.x or 3.x on them (eg., OSX). seq has the nice property that writing a shell or executable replacement is easy.

I think the 2nd example is fine. It might be cut and pasted from a POSIX example.


Those are fair points. Another nit I just noticed in the second example is the extraneous semicolon.


>you should be using bash's sequence expression anyway

I'd love to, but it inexplicably doesn't support variables. You have to hard code the range and step. Pretty much useless for most of my use cases.


I'd like that too.

At this stage it's no better than a meme: https://groups.google.com/d/msg/comp.os.linux.misc/w0bcKoEye...

I know that if you mention the TLDP stuff in #bash you get yelled at, though.


The TLDP bash guide is full of a range of submitted examples that run the gamut of good and bad practices. That said, it's still one of the better resources out there, and I have the reference page bookmarked. But this is Bash we're talking about, "best" practice is a relative term.


To be fair, searching it in Google also gives TLDP pages as the top results. I think TLDP just has better SEO.


It just has more nodes pointing to its links.

That's the danger behind PageRank. If something false is popular enough, it will replace fact.


Is there any alternative, though? You can't algorithmically determine truth.


Not without sentiment analysis applied to links. However, that's game-able.


Not just more nodes, then it would have been easy, but more "credible and relevant" nodes. At least that's the idea.


correct. simple back-links no longer have the weight they used to (and in some situations can actually harm your SEO).

Backlinks must come from other reputable (in google's algorithm's eye's) sites before they carry any weight.

So, this would mean, tldp.org has a lot of top-ranked linux/bash and related sites pointing to it's pages.


I learned bash using tldp years ago, never had any major issues.


It seems like DDG is positioning itself to become WolframAlpha for tech. Hopefully, this will allow it to better catch the exact market segment that would be wary of search engine tracking (educated tech-savvy people).


Nice! It's too bad that this doesn't actually evaluate the expression in a sandbox, but nonetheless it's a very handy syntax guide.

It'd be nice to see option descriptions extracted from manpages: searching for "df -h" could then produce:

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)


http://explainshell.com is what you're looking for.


I'm aware of it; I'd love to see that functionality integrated into DDG.


Actually one of their guys approached me last time explainshell appeared on HN. I think he suggested I implement it as some sort of shortcut, but I haven't had time to look into it.

I have a branch somewhere that exposes the results as JSON, which should make it easy to integrate if they were interested.


I wonder if the developer of explainshell is reading. It looks like development has stalled. It's a really nice idea and could serve as a powerful backend for DDG.


I am reading this!

Admittedly I haven't had a lot of time recently to improve explainshell. But I haven't deserted it; it's just a matter of finding a block of free days to focus on it.

(and as always, patches are welcome ;)


[deleted]


There is one in its own file: https://github.com/idank/explainshell/blob/master/TODO

It's slightly out of sync with the github issues though.


Deleted my comment 10 seconds after you posted yours, I must be blind :P Looked straight past it.


What a great resource! I did not know about this before today!


I note that queries like https://duckduckgo.com/?q=man+df put in a box from LinuxCommand.org


This is pretty awesome, but I don't know if people often phrase their queries like that, and I'm not sure whether it's worth it to start phrasing it like this. It doesn't seem to support more complex bash queries, so I'm not really sure how often this will be used.


[ is part of Bash syntax


Nooo! Mistake #0 about shell scripting is thinking [ is syntax. It makes it way more weird and inexplicable. [ is a command, just like any other. That's why there needs to be a space between it and the next thing, and why you have to use weird looking flags and such. This is why I prefer the command `test`. It does the same thing as `[`, but is more obviously a command. And when you see it as a command, it's obvious that any command can be used in an if directly; if is just checking the exit code. (Whenever I see someone doing `if [ $? -ne 0 ]`, it makes me cry.)


I'll bite. What should we be using instead of `if [ $? -ne 0 ]`? Because that's pretty standard in my (admittedly very basic) bash.


Instead of

somecommand

if [ $? -ne 0 ]; then echo "it failed"; fi

the parent commenter presumably wants to see

if ! somecommand; then echo "it failed"; fi


Or:

    somecommand || echo "it failed"
if it's just one statement. There are some cases where it just looks more natural. For example:

    start_service || log "already started"


> when you see it as a command, it's obvious that any command can be used in an if directly; if is just checking the exit code

One of the best days of my life


test is [


Indeed, "test" is exactly like "[" except that it doesn't check that its last argument is a "]".


For some reason I've always been amused by "which [". The idea of 'punctuation' being a command or a filename is something that threw me.


...and perhaps surprisingly also a program sitting on disk.

    $ type [
    [ is a shell builtin
    $ [ 1 -gt 0
    bash: [: missing `]'

    $ whereis [
    /bin/[
    $ /bin/[ 1 -gt 0
    # success


You got a root shell out of `/bin/[` ? Impressive :)


Hm, which version of [ is that? I get:

    $ /usr/bin/[ 1 -gt 0 && echo success
    /usr/bin/[: missing `]'
    $ /usr/bin/[ 1 -gt 0 ] && echo success
    success
(This is from coreutils 8.13-3.5 on Debian GNU/Linux)


What ships in OS X 10.9, presumably BSD-derived.


There should a dollar sign before hello, e.g.:

bash [ -z $hello ]

...or is that a bashism that I'm not aware of? If so, that's just horrible.

Personally, I prefer to write portable shell scripts. Entering "!posix test" into DuckDuckGo to obtain the man page for "test" yields far more meaningful information for the same problem.


[ -z hello ] is perfectly fine, but hello is not a variable, it's the string "hello". It will always return false. ddg doesn't seem to evaluate the command, just explicit it.


> It will always return false.

Exactly! It's not the best of examples really, is it?


"results to true if the length of 'hello' is zero." I'm glad they cleared that up. Otherwise, I might have assumed that there was no case where 'hello' had a length of zero. Joking aside, it is a useful syntax guide.


Damn, should draw FSA fors RegExps.

Like this

https://www.debuggex.com/r/IC3q9sEdkKjhuLWU

And explains it like this

http://regex101.com/


As an aside, I think the new design is perfect. Good job on iterating on the feedback!


More please! e.g.

     bash $?
is not explained.


If you're interested, you can add that in to https://github.com/duckduckgo/zeroclickinfo-goodies/blob/mas... since it's open source.


I've been reading through some of those goodies, and they all seem pretty cool, but I'm wondering how secure using the 4 word random passphrase generator[0] would be...

[0] https://github.com/duckduckgo/zeroclickinfo-goodies/blob/mas...


Not at all secure if you run it on DDG, since you give your password to a third party before you even get it yourself.


But of course you could run it ten times and pick one of them. Or modify the instant answer to return ten or twenty. Not ideal or optimal, but there it is.


That hardly helps at all. Now instead of knowing your exact password, your attacker knows that your password is one of these 10-20 entries, and it's easy to just try them all.


Nobody has ever suggested this is a secure or ideal way to do it..


My point is that it's not really even better.


I'm always finding another bash magic variable. '$?' is 'exit code of last command'. Last week I found '$-', which tells you which flags are currently set (set -x, set -e type of thing).


They are all listed in the man page, thankfully.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: