Unix and Xcode question

Discussion in 'Mac Programming' started by mdeh, Feb 11, 2009.

  1. mdeh macrumors 6502

    Jan 3, 2009
    I am trying to call the executable of a program ( ObjC) ( Very simple ) called 16.1 from the command line with these commands. ( Given that the following is in a terminal window)

    MB-Pro Debug m$ pwd
    MB-Pro Debug m$ ls -F
    MB-Pro Debug m$ /Users/m/Xcode_practise/16.1/build/Debug/16.1
    2009-02-11 20:51:09.918 16.1[499:10b] Hello, World!
    MB-Pro Debug m$ 16.1
    -bash: 16.1 command not found

    So, if I am correct, I am in the PWD when calling the executable, but it only responds to the full - path. Could someone perhaps explain why this is so, in other words, why the invocation "16.1" will not work?

    Thanks as usual in advance.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I can't remember the defaults in OS X, but it is safest to exclude the current directory (.) from your path. This is more of a concern on multiuser systems, where someone can put a file called ls somewhere that actually deletes everything in your home directory. It's still a good idea on any system, so when you need to run an executable in your current directory, just run ./progname. In this case

  3. mdeh thread starter macrumors 6502

    Jan 3, 2009

    Lee...could you elaborate just a little ( and thanks for that reply) what exactly I am calling with
    I assume??:) that
    is the current directory ( as you say), the slash just separates the directory from the executable? If so, why can the shell not find the executable in the current directory when it is called alone? For example,
    just calls the calendar.
    Thanks in advance.
  4. plinden macrumors 68040


    Apr 8, 2004
    lee1210 is correct in that the current directory is omitted from the $PATH environment variable (unless you're in a directory which is in $PATH).

    You can see $PATH by typing: echo $PATH - notice that's it's a series of directories. When typing a command, OS X (and any other *nix OS) looks through the contents of $PATH from start to end and executes the first matching command it meets.

    Not including the current directory is a security measure. Commands such as ls or cd are in /usr/bin, which is in $PATH, but if someone fooled you into downloading an executable called cd that is in the path before /usr/bin, it would execute before the correct cd command.

    The PATH could include the current directory at the end (and you can set PATH to include it) but t
    yping ./command specifically tells the OS to run command that's in the current working directory, which is something you have to think about so is not something you would do accidentally.
  5. Doctor Q Administrator

    Doctor Q

    Staff Member

    Sep 19, 2002
    Los Angeles
    When you type a command, the shell notices the command is just a name or if it is a path, which means a string with a / in it.

    If it has a /, it uses that path to find the file.

    If it doesn't have a slash, and it's just a name, the shell looks in a collection of folders for a file with that name. It executes the first matching file. This collection of folders is called your path, and can be see by typing
    echo $PATH
    You can set the value of PATH to be any collection of folders, although the way you set it depends on which shell you are using. Yes, there's a choice of shells.

    But back to the case where you type a command with a path. If the path starts with a /, it is an "absolute" path and tells the shell exactly where on disk your program file is, from the top of the disk structure on down.

    If it has a / in the middle, but not at the beginning, then its a "relative" path, and tells the shell where to find the program file starting in the folder you are currently in. The command
    tells where you currently are.

    Now we can explain your case.

    When you type
    the shell is looking in a collection of folders from your path, but the folders apparently don't include the current folder. If you changed your path to include the current folder, it would find your "16.1" program.

    If you type
    as lee1210 suggests, you are giving a relative path, so the shell will look exactly one place: where you said. By convention, "." means "the folder I'm in right now, so ./16.1 means the program file named 16.1 in the current folder!
  6. mdeh thread starter macrumors 6502

    Jan 3, 2009
    thank you for that wonderful explanation. Much appreciated.
  7. mdeh thread starter macrumors 6502

    Jan 3, 2009
    Thank you for that great info.
  8. mdeh thread starter macrumors 6502

    Jan 3, 2009
    Lee...thank you again. Your answer makes (more) sense to me now...and, btw, thanks for always trying to answer my queries. It's people like you and the others that make this foray into programming more pleasant.

Share This Page