Information and Links
Join the fray by commenting, tracking what others have to say, or linking to it from your blog.
No equivalent to strace on OS X and the mysterious left square bracket
Tonight a friend pointed out that there is a weird file in /bin on OS X. If you do 'ls -la /bin' you will most probably see a file called [. That is not a typo, the file has the left square bracket as a name.
Curious off the bat about what it does, I fired up strace and ran the [ file. Except strace doesn't exist on OS X. strace lets you trace system calls and signals on a running file or a file that you run. Basically strace lets you watch what a file is doing. This can be very useful to solve mysterious problems.
So if you are on OS X and you need strace, you can always use ktrace instead. It works mostly the same it seems.
As for the /bin/[ file, it didn't seem to do anything when I ran 'ktrace /bin/['
If I use otool to see what [ depends on it gives me:
running strings - /bin/[ gives me long list:
-
[root scripts] strings - /bin/[
-
__PAGEZERO
-
__TEXT
-
__text
-
__TEXT
-
__cstring
-
__TEXT
-
__textcoal_nt
-
__TEXT
-
__DATA
-
__data
-
__DATA
-
__dyld
-
__DATA
-
__const
-
__DATA
-
__common
-
__DATA
-
__IMPORT
-
__pointers
-
__IMPORT
-
__jump_table
-
__IMPORT
-
__LINKEDIT
-
/usr/lib/dyld
-
/usr/lib/libgcc_s.1.dylib
-
/usr/lib/libSystem.B.dylib
-
?8!u
-
?8!uu?x
-
__dyld_make_delayed_module_initializer_calls
-
__dyld_mod_term_funcs
-
%s: %s
-
%s: out of range
-
%s: bad number
-
argument expected
-
closing paren expected
-
missing ]
-
unknown operand
-
_NXArgc
-
_NXArgv
-
___progname
-
__mh_execute_header
-
_catch_exception_raise
-
_catch_exception_raise_state
-
_catch_exception_raise_state_identity
-
_clock_alarm_reply
-
_do_mach_notify_dead_name
-
_do_mach_notify_no_senders
-
_do_mach_notify_port_deleted
-
_do_mach_notify_send_once
-
_do_seqnos_mach_notify_dead_name
-
_do_seqnos_mach_notify_no_senders
-
_do_seqnos_mach_notify_port_deleted
-
_do_seqnos_mach_notify_send_once
-
_environ
-
_receive_samples
-
__DefaultRuneLocale
-
___error
-
___keymgr_dwarf2_register_sections
-
___maskrune
-
__cthread_init_routine
-
_abort
-
_access
-
_atexit
-
_errno
-
_errx
-
_exit
-
_getegid
-
_geteuid
-
_isatty
-
_lstat
-
_mach_init_routine
-
_stat
-
_strcmp
-
_strtol
-
8__PAGEZERO
-
__TEXT
-
__text
-
__TEXT
-
__symbol_stub
-
__TEXT
-
__picsymbol_stub__TEXT
-
$__symbol_stub1
-
__TEXT
-
__cstring
-
__TEXT
-
__picsymbolstub1__TEXT
-
__DATA
-
__data
-
__DATA
-
__nl_symbol_ptr
-
__DATA
-
__la_symbol_ptr
-
__DATA
-
__dyld
-
__DATA
-
__const
-
__DATA
-
__common
-
__DATA
-
8__LINKEDIT
-
-
/usr/lib/dyld
-
/usr/lib/libgcc_s.1.dylib
-
/usr/lib/libSystem.B.dylib
-
#x|y
-
}"Kx@
-
9),?H
-
}"Kx
-
<8c,
-
@8c,
-
88c,
-
H8c,
-
L8c,
-
P8c-
-
-<N?
-
9k-4
-
D8c-HH
-
x?B0
-
k0d}i
-
3x8`
-
X|BJ
-
__dyld_make_delayed_module_initializer_calls
-
__dyld_image_count
-
__dyld_get_image_name
-
__dyld_get_image_header
-
__dyld_NSLookupSymbolInImage
-
__dyld_NSAddressOfSymbol
-
libobjc
-
__objcInit
-
__dyld_mod_term_funcs
-
%s: %s
-
%s: out of range
-
%s: bad number
-
argument expected
-
closing paren expected
-
missing ]
-
unknown operand
-
_NXArgc
-
_NXArgv
-
___progname
-
__mh_execute_header
-
_catch_exception_raise
-
_catch_exception_raise_state
-
_catch_exception_raise_state_identity
-
_clock_alarm_reply
-
_do_mach_notify_dead_name
-
_do_mach_notify_no_senders
-
_do_mach_notify_port_deleted
-
_do_mach_notify_send_once
-
_do_seqnos_mach_notify_dead_name
-
_do_seqnos_mach_notify_no_senders
-
_do_seqnos_mach_notify_port_deleted
-
_do_seqnos_mach_notify_send_once
-
_environ
-
_receive_samples
-
__DefaultRuneLocale
-
___error
-
___keymgr_dwarf2_register_sections
-
___maskrune
-
__cthread_init_routine
-
_abort
-
_access
-
_atexit
-
_errno
-
_errx$LDBL128
-
_exit
-
_getegid
-
_geteuid
-
_isatty
-
_lstat
-
_mach_init_routine
-
_stat
-
_strcmp
-
_strtol
So, I have no more time to investigate further right now, and Google shows nothing useful. If anyone has an idea of what the mysterious [ binary does, please let me know. Thanks!


If you’re know basic Bourne-shell script (/bin/sh), you’ll be familiar with the “if” command, the basic syntax of which is:
if ; then
…
fi
While can be anything, it’s typically a boolean test of some sort. While you could write it like this:
if test $a = 1; then
echo foo
fi
We typically write it like this instead:
if [ $a = 1 ]; then
echo foo
fi
The ‘[‘ is simply an alias for ‘test’ used to make shell scripts more readable. These days, most shells have ‘test’ (and ‘[‘) as built-ins, but in the “old days” this was an external command.