(Bash) You Are Using rsync Incorrectly And You Will Lose Everything Unless You Read This Now!

rsync is generous in its functionality and flexible in how you use it. The only bad things it can do are what you tell it. The most important thing that you need to know immediately about rsync is that you can get a preview of what you are telling it to do before it actually does it. Here is how

Continue reading “(Bash) You Are Using rsync Incorrectly And You Will Lose Everything Unless You Read This Now!”

(macOS+Bash) Find All Locations Of A Binary Using `type’ not `which’

I can’t find the git 2.10 binary so I run

which git
/usr/local/bin/git

Excellent, found it.

And then I check it’s version to verify I am looking at the right version

/usr/local/bin/git --version
git version 2.15.1

Oops that isn’t what I wanted.

There must be another git getting loaded earlier in the search path.

Here is how to find out where all copies of git live

type -a git
git is /usr/local/bin/git
git is /usr/local/bin/git
git is /usr/bin/git

Look for the right git at version 2.10

/usr/local/bin/git --version
/usr/bin/git --version
git version 2.15.1
git version 2.10.1 (Apple Git-78)

I want the second one.

That is how to track down the location of a binary file on macOS that appears multiple times in the search path.

(Bash) Avoid Most Bash Errors Pretty Much By Quoting Everything, And More Best Practices

This page shows common errors that Bash programmers make. These examples are all flawed in some way.

You will save yourself from many of these pitfalls if you simply always use quotes and never use WordSplitting for any reason! Word splitting is a broken legacy misfeature inherited from the Bourne shell that’s stuck on by default if you don’t quote expansions. The vast majority of pitfalls are in some way related to unquoted expansions, and the ensuing word splitting and globbing that result.

All of us have fallen into one of these pits.

Why not use Python instead? :)

Oh yea, non-interactive shells 😠.

More on Quotes and Word Splitting.

Fix For When Bash Can’t Find Its Debugger On macOS

Sometimes Bash needs its debugger but it can’t find it. It looks like this:

/usr/share/bashdb/bashdb-main.inc: No such file or directory
-bash: warning: cannot start debugger

For example when you start bashdb yourself with bash --debugger or set shopt -s extdebug. Another example is when your terminal turns it on.

Here is the fix that worked on my box:

brew install bashdb
bashhome=$(brew --prefix bash)
bdbhome=$(brew --prefix bashdb)
ln -s $bdbhome/share/bashdb $bashhome/share/bashdb
unset bashhome
unset bdbhome

Update OSX 10.9 to use bash4

Source.

Script started on Sat Jun 21 12:25:07 2014
ESC[~> echo $BASH_VERSION
3.2.51(1)-release
gcr@orion:~> which bash
/bin/bash
gcr@orion:~> brew info bash
bash: stable 4.3.18, HEAD
http://www.gnu.org/software/bash/
Not installed
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/bash.rb
ESC[1;34m==>ESC[1;39m DependenciesESC[0m
Required: ESC[1;32mreadlineESC[0m
ESC[1;34m==>ESC[1;39m CaveatsESC[0m
In order to use this build of bash as your login shell,
it must be added to /etc/shells.
gcr@orion:~> brew install bash
[1;34m==>ESC[1;39m PatchingESC[0m
patching file arrayfunc.c
patching file bashline.c
patching file externs.h
patching file jobs.c
patching file lib/glob/glob.c
patching file lib/glob/gmisc.c
patching file lib/readline/display.c
patching file lib/readline/readline.c
patching file lib/sh/shquote.c
patching file parse.y
patching file patchlevel.h
patching file pcomplete.c
patching file subst.c
patching file test.c
patching file trap.c
patching file variables.c
patching file y.tab.c
ESC[1;34m==>ESC[1;39m ./configure --prefix=/usr/local/Cellar/bash/4.3.18 --with-installed-readlineESC[0m
ESC[1;34m==>ESC[1;39m make installESC[0m
ESC[1;34m==>ESC[1;39m CaveatsESC[0m
In order to use this build of bash as your login shell,
it must be added to /etc/shells.
ESC[1;34m==>ESC[1;39m SummaryESC[0m
/usr/local/Cellar/bash/4.3.18: 59 files, 7.4M, built in 41 seconds
gcr@orion:~> /usr/local/bin/bash --versinESCESC[Kon
GNU bash, version 4.3.18(1)-release (x86_64-apple-darwin13.2.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
gcr@orion:~> sudo echo "/usr/local/bin/bashbug" >> /etc/shells
bash: /etc/shells: Permission denied
gcr@orion:~> sudo vi /etc/shells
Password:
ESC[?1049hESC[?1hESC=ESC[1;25rESC[?12;25hESC[?12lESC[?25hESC[27mESC[mESC[HESC[2JESC[?25lESC[25;1H"/etc/shells" 10L, 179CESC[>cESC[1;1H# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
gcr@orion:~> chsh

Then login again

gcr@orion:~> which bash
/usr/local/bin/bash
gcr@orion:~> echo $BASH-VESCESC[KESCESC[K_VERSION
4.3.18(1)-release
gcr@orion:~> bash --version
GNU bash, version 4.3.18(1)-release (x86_64-apple-darwin13.2.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
gcr@orion:~> exit