News for hackers. Stuff that matters.
Re:Bash [ -z hello ] by Nyubis on Wednesday June 25, 2014 @01:59PM (#7945650)
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.
Re:Bash [ -z hello ] by mattdanger on Wednesday June 25, 2014 @03:01PM (#7946079)
[ is part of Bash syntax
Re:Bash [ -z hello ] by embolalia on Wednesday June 25, 2014 @03:57PM (#7946415)
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.)
Re:Bash [ -z hello ] by jsmeaton on Wednesday June 25, 2014 @06:45PM (#7947188)
I'll bite. What should we be using instead of `if [ $? -ne 0 ]`? Because that's pretty standard in my (admittedly very basic) bash.
Re:Bash [ -z hello ] by schoen on Wednesday June 25, 2014 @07:06PM (#7947276)
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

Re:Bash [ -z hello ] by viraptor on Wednesday June 25, 2014 @07:26PM (#7947325)
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"
Re:Bash [ -z hello ] by JetSpiegel on Wednesday June 25, 2014 @08:06PM (#7947466)
> 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

Re:Bash [ -z hello ] by manicdee on Wednesday June 25, 2014 @06:30PM (#7947133)
test is [
Re:Bash [ -z hello ] by kl4m on Wednesday June 25, 2014 @09:14PM (#7947647)
Indeed, "test" is exactly like "[" except that it doesn't check that its last argument is a "]".
Re:Bash [ -z hello ] by vacri on Wednesday June 25, 2014 @11:22PM (#7947981)
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.
Re:Bash [ -z hello ] by frou_dh on Wednesday June 25, 2014 @03:36PM (#7946291)
...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
Re:Bash [ -z hello ] by mappu on Wednesday June 25, 2014 @04:59PM (#7946727)
You got a root shell out of `/bin/[` ? Impressive :)
Re:Bash [ -z hello ] by e12e on Wednesday June 25, 2014 @09:02PM (#7947614)
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)
Re:Bash [ -z hello ] by frou_dh on Thursday June 26, 2014 @03:06AM (#7948443)
What ships in OS X 10.9, presumably BSD-derived.