What the hell is/USR/BIN/CD?

Source: Internet
Author: User
Tags builtin posix

As mentioned earlier, I found many external commands on my Mac that have the same name as the Bash internal commands, and in those 24 external commands I found a strange phenomenon: 15 of them were shell scripts, and, oddly enough, they were hard links to the same shell script:

$ find/usr/bin-inum 376183

/usr/bin/alias

/usr/bin/bg

/usr/bin/cd

/usr/bin/command

/usr/bin/fc

/usr/bin/fg

/usr/bin/getopts

/usr/bin/hash

/usr/bin/jobs

/usr/bin/read

/usr/bin/type

/usr/bin/ulimit

/usr/bin/umask

/usr/bin/unalias

/usr/bin/wait

Look at the contents of the script:

$ cat/usr/bin/cd

#!/bin/sh

# $FreeBSD: Src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 Cperciva EXP $

# This file was in the public domain.

Builtin ' echo ${0##*/} | TR \[:upper:] \[:lower:] ' ${1+ ' [email protected] '}

The script has only one row, what does it do? I analyzed that when the user entered a normal internal command such as a CD, the shell would certainly use it as an internal command, only the user accidentally entered the CD into a CD, because the Mac file system is not case-sensitive, the shell will execute the external script. The script takes the value of $, which is/USR/BIN/CD, cuts the path, and then converts the size letter to lowercase, which is the CD, then executes the CD, with the parameters. But I have a few points to think about, the builtin here is completely redundant, ${1+ "[email protected]"} can also be simply written "[email protected]", most importantly, the execution of these scripts is almost meaningless, because the Shell The script is executed in the new shell process of the current shell process, that is, the execution CD/equivalent to execute bash-c ' CD/', the current shell's working directory does not change, in addition to the CD, other commands are the same, although executed, but completely useless, I use Alia S and Unalias Demo:

$ Alias ' ll=ls-l '

$ Alias

$ unalias LL

/usr/bin/unalias:line 4:unalias:ll:not found

$ alias

Alias Ll= ' Ls-l '

This is what the starting point is, the input CD should be an error only to Ah, do not give an error but did not effect, many people confused behavior.

So I looked up the internet and found a Japanese article perfect detail the context of the script. The reason for this script to exist is that the POSIX standard requires the operating system to provide external commands for these 14 internal commands, such as Env, find, nice, nohup, time, Xargs, and so on, such as Env CD. POSIX and why so rules, then do not know, but really no eggs use Ah, no wonder that Linux has not complied with this specification.

Originally this script was born on FreeBSD, why add builtin and use ${1+ "[email protected]"} is because of the FreeBSD operating system reason, that article has said. There is also the original script is not tr \[:upper:] \[:lower:] This part of the FreeBSD does not need this, the Apple transplant when considering their own file system is not case-sensitive, deliberately made improvements.  

What the hell is/USR/BIN/CD?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.