LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2014, 05:26 PM   #1
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 877

Rep: Reputation: 124Reputation: 124
Question Just Curious: why do "/usr/bin/test" and "/usr/bin/[" have different sizes?


This isn't a pressing concern, but I just had a "WTF?!" moment and thought I should share the love.

Code:
dane@orchestrator ~ $ which [
/usr/bin/[
dane@orchestrator ~ $ ls -lh `which [`
-rwxr-xr-x 1 root root 35K Nov 19  2012 /usr/bin/[*
dane@orchestrator ~ $ ls -lh `which test`
-rwxr-xr-x 1 root root 31K Nov 19  2012 /usr/bin/test*
dane@orchestrator ~ $ dpkg-query -S /usr/bin/[
coreutils: /usr/bin/[
dane@orchestrator ~ $ dpkg-query -S /usr/bin/test
coreutils: /usr/bin/test
So...how does this work?

(Have fun!)

--Dane
 
Old 01-04-2014, 05:53 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,221
Blog Entries: 11

Rep: Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172
Because they are not the identical.

That sounds like a tautology, but it is not.

Among their differences are their usage syntax and the fact that [ honors --help and --version options while test does not.

So while we speak and think of them as being the same thing, they really are not (at least the GNU versions).

Last edited by astrogeek; 01-04-2014 at 05:58 PM.
 
1 members found this post helpful.
Old 01-05-2014, 07:20 AM   #3
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 877

Original Poster
Rep: Reputation: 124Reputation: 124
Are there functional differences, other than --help and --version?
 
Old 01-05-2014, 07:46 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
A heads up about test, test and [ (yep, test is mentioned twice).

There is also a bash internal command called test, which is actually used when not using the full path to the test binary (/usr/bin/test). See man bash, the SHELL BUILTIN COMMANDS section.

The binary [ and binary test work the same way (except for the --version/--help), as you might have noticed both point to the saem manual page.

The binary and the build in do have some differences (availability and names of options).
 
3 members found this post helpful.
Old 01-06-2014, 12:54 AM   #5
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 877

Original Poster
Rep: Reputation: 124Reputation: 124
Ah, excellent distinction! Thanks, druuna.
 
Old 01-06-2014, 03:33 AM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 505

Rep: Reputation: 224Reputation: 224Reputation: 224
Take a look at source code of test and [ for yourself. The only difference starts at line 837.

The reason test does not recognise any options is that it would not be possible to distinguish whether the option was actual option or a test. I.e. “test "$FOO"” is a valid way to test whether the variable FOO is empty or not, however if FOO was “--help” or “--version”, the test would result in superfluous output.

[ can support options since its valid invocation requires “]” at the end of the command.

Quote:
Originally Posted by druuna View Post
There is also a bash internal command called test, which is actually used when not using the full path to the test binary (/usr/bin/test). See man bash, the SHELL BUILTIN COMMANDS section.
There is also a bash built-in “[” (which does not support “--help” or “--version” by the way).

PS. This of course does not change the fact that both binaries could be the same and the behaviour could be changed based on “argv[0]”. I assume this is not the cases to speed things up slightly.

Last edited by mina86; 01-06-2014 at 03:36 AM.
 
1 members found this post helpful.
  


Reply

Tags
bash, curiosity, humor, scripting, wtf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cannot 'find "[" -maxdepth 1 -wholename "/usr/bin/[" ! \( -perm /g+w,o+w \)' standard_output Linux - General 5 09-13-2013 04:23 PM
What does [if test "/usr/local/bin/php" = '@'php_bin'@'; then] test for zhjim Programming 3 11-02-2009 02:33 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 03:30 PM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM
what is "S" instead of "X" in the file permission when i look at /usr/bin/chsh? Linux_interest Linux - Newbie 4 08-28-2004 09:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration