News for hackers. Stuff that matters.
Re:Stupid Unix Tricks by asaph on Thursday October 17, 2019 @10:58AM (#21282727)
> Don’t try to install things with brew if brew is not installed:

   if which brew 2>&1 /dev/null ; then
        brew install jq
   fi
This just hides a useful error message (brew not installed). I would rather just see that error message (either interactively or in a log) and have the script fail. Hiding the error message just leads to an eventual failure down the road when jq is invoked.
Re:Stupid Unix Tricks by reacweb on Thursday October 17, 2019 @11:24AM (#21283037)
In bash, there is the build-in command named "command". I have used it for the same purpose, like in:

    command -v brew >/dev/null && brew install jq
Re:Stupid Unix Tricks by temo4ka on Thursday October 17, 2019 @02:30PM (#21285014)
If you don't care for maximum POSIX compatibility, i.e. your script is bash-specific, it's better to use "hash", which is going to ignore aliases (but not functions) and also has the benefit of caching the command for further use.

  hash foo &>/dev/null || { echo "foo command not found blabla..." >&2; exit 1; } 

more info: https://stackoverflow.com/questions/592620/how-to-check-if-a...
Re:Stupid Unix Tricks by raldi on Thursday October 17, 2019 @03:03PM (#21285323)
Wow. It's impossible to google, and on MacOS, `man command` is useless.
Re:Stupid Unix Tricks by s_gourichon on Thursday October 17, 2019 @03:10PM (#21285385)
$ help command

command: command [-pVv] command [arg ...] Execute a simple command or display information about commands.

    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.
    
    Options:
      -p    use a default value for PATH that is guaranteed to find all of
            the standard utilities
      -v    print a description of COMMAND similar to the `type' builtin
      -V    print a more verbose description of each COMMAND
    
    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.
Re:Stupid Unix Tricks by m463 on Thursday October 17, 2019 @10:15PM (#21288080)
Thank you.

I never knew about help to describe a builtin shell command. normally searching for something like "command" in the bash man page would be very tedious.

also: help echo

Re:Stupid Unix Tricks by dTal on Saturday October 19, 2019 @07:50AM (#21298000)
How little we expect from our computers. "help <command>" should be the first thing you expect to work.
Re:Stupid Unix Tricks by imihai1988 on Friday October 18, 2019 @03:54AM (#21289451)
i do think 'man command' should bring up the specific man page for this, so there is no need to search into the bash man. this works with most builtin shell commands. e.g. 'man ls' is a thing
Re:Stupid Unix Tricks by stragies on Friday October 18, 2019 @04:57AM (#21289744)
On (at least) Debian systems (, and probably many more) `ls` is not a built-in, but an external binary (`/usr/bin/ls`) is used.

`which ls` and `help ls` will show you, if it is similar on your system.

Re:Stupid Unix Tricks by imihai1988 on Friday October 18, 2019 @12:19PM (#21292659)
It does't need to be a built-in to have a man page (many first party and third party libraries come with their own man pages).
Re:Stupid Unix Tricks by m463 on Friday October 18, 2019 @03:46PM (#21294509)
"man command" on linux says "no manual entry for command"

"man echo" on linux describes /usr/bin/echo "help echo" describes the builtin.

on the othe hand, "man command" on mac os x gives a huge manpage of builtins (still hard to search for the common word "command")

Re:Stupid Unix Tricks by gravitas on Thursday October 17, 2019 @05:40PM (#21286607)
`command` is a bash builtin, not an external program. The information is in the bash man page (which is hard to find in there because the word "command" is in the man page 7 bajillion times. Look in the major section "SHELL BUILTIN COMMANDS" where you find `cd` and others like it).
Re:Stupid Unix Tricks by jplayer01 on Friday October 18, 2019 @04:33AM (#21289617)
You can determine if something is a bash built-in by using 'which command'. If it's built-in, man won't work.
Re:Stupid Unix Tricks by Fnoord on Friday October 18, 2019 @05:19AM (#21289830)
In config.fish I use type for this purpose, together with the exit code

  test (type brew 2>/dev/null); and brew # blahblah
Re:Stupid Unix Tricks by lloeki on Saturday October 19, 2019 @05:01AM (#21297546)
type -p also works in bash/zsh
Re:Stupid Unix Tricks by Fnoord on Saturday October 19, 2019 @05:44AM (#21297659)
Fish as well, but the redirection of both stdout and stderr is different:

  type -p brew >/dev/null ^&1; and brew
in Bash it would be:

  type -p brew &>/dev/null && brew
Using test avoids caring about stdout/stderr.
Re:Stupid Unix Tricks by aasasd on Thursday October 17, 2019 @11:53AM (#21283347)
You don't need to check for `brew` in each task if they're run automatically. Just have one task that checks for `brew`.

With a proper dependency graph, the tasks installing things would depend on the task `brew-installed`.

This, of course, is leaving alone the point that installing stuff on startup is weird, doubly so with brew which is pretty slow.

Re:Stupid Unix Tricks by aasasd on Friday October 18, 2019 @01:29AM (#21288940)
Correction, to clarify: you need to check for `brew` in each task but not warn. One master warning is enough.
Re:Stupid Unix Tricks by jolmg on Thursday October 17, 2019 @11:37AM (#21283185)
It seems you miswrote, or the author corrected himself. Right now it's:

  if which brew >/dev/null 2>&1 ; then
      brew install jq
  fi
Which would hide the error message. The code you posted outputs:

  brew not found
  /dev/null not found
Re:Stupid Unix Tricks by lmm on Thursday October 17, 2019 @12:12PM (#21283514)
It's impractical to determine whether Hacker New's undocumented formatting language is going to eat any given angle bracket ahead of time. I suspect OP wrote something correct and the site has mangled it.
Re:Stupid Unix Tricks by asaph on Thursday October 17, 2019 @09:56PM (#21287999)
It got messed up when I was trying to format it, unfortunately. But my point still stands.
Re:Stupid Unix Tricks by majewsky on Thursday October 17, 2019 @11:15AM (#21282916)
I think this snippet needs some context. In all contexts that I can imagine, the first line should actually be

  if which jq &>/dev/null; then
    brew install jq
  fi
Re:Stupid Unix Tricks by playpause on Thursday October 17, 2019 @11:23AM (#21283013)
That seems more reasonable, but it's not what the author wrote. He precedes the snippet with "Don’t try to install things with brew if brew is not installed", so his intention does seem to be to swallow errors silently, which is definitely weird.
Re:Stupid Unix Tricks by pletnes on Thursday October 17, 2019 @02:41PM (#21285115)
He might want his .bashrc to work on both macos and linux.
Re:Stupid Unix Tricks by 00N8 on Thursday October 17, 2019 @10:31PM (#21288157)
IME .bashrc already doesn't work the same on OS X, though, so that's still a weird reason.
Re:Stupid Unix Tricks by zeroimpl on Thursday October 17, 2019 @10:50PM (#21288250)
Likely due to using the older version of bash preinstalled on Mac. Installing a new one and it should be virtually identical.