Find the answer to your Linux question:
Results 1 to 5 of 5
Why do some linux commands have to be run with "./". Is there something I can do to take a command that runs as ./* and run it as *. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9

    Linux commands and ./


    Why do some linux commands have to be run with "./".
    Is there something I can do to take a command that runs as ./*
    and run it as *.
    Thanks

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    When you enter a command without a path the OS looks at variable PATH to find the directory the command is in. For example if you enter '# ls' it'll find it in /bin because that directory is part of PATH.

    If the directory you're in isn't in PATH then you have to use ./ so the OS knows to find command in the current directory.

    To avoid having to use ./ you have to add that directory to PATH. PATH is probably defined in script .bash_profile in your home directory. PATH is a colon( : ) separated list like this:

    # echo $PATH
    /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/username/bin

    Just edit that script and add the directory to the end of it preceding it with a colon. You either have to log out and back in or run the script manaully (# ~/.bash_profile) for the change to take affect.

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    I'm running Ubuntu 8.04 and was told to put startup scripts in the /etc/init.d directory. If I move the file to /usr/bin and create a link in /etc/init.d will that work?

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    No that won't work. Startup scripts are executed only during system bootup. But if /usr/bin is part of PATH (and it probably is) then it should work without making any other changes. Check your PATH with:

    # echo $PATH

    I personally would move the file to /usr/local/bin.

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by vsemaska View Post
    When you enter a command without a path the OS looks at variable PATH to find the directory the command is in. For example if you enter '# ls' it'll find it in /bin because that directory is part of PATH.

    If the directory you're in isn't in PATH then you have to use ./ so the OS knows to find command in the current directory.
    Right.

    To avoid having to use ./ you have to add that directory to PATH. PATH is probably defined in script .bash_profile in your home directory. PATH is a colon( : ) separated list like this:
    This depends on the shell. For bash, it can be .bash_profile for login sessions, and .bashrc for non-login ones. There are many more files that bash parses, they all can be seen in the bash man page, section "INVOCATION".

    If you use any other shell then you need to check the man page for your shell instead of bash.

    Just edit that script and add the directory to the end of it preceding it with a colon. You either have to log out and back in or run the script manaully (# ~/.bash_profile) for the change to take affect.
    Nope. It won't work because when you launch a shell it's run into a separate shell session. Everything that happens into that new shell session will not survive once the script is over and that session is closed.

    You have many alternatives though:

    1. Log off and back in.
    2. Source the file, instead of running it. This is done with "source ~/whatever" or just a dot, this way ". ~/whatever".
    3. Fork a new shell: "exec bash" will close your old shell and reopen a new one. As a side effect, the relevant config files will be re-read since a new shell is spawned.



    Quote Originally Posted by frippee View Post
    I'm running Ubuntu 8.04 and was told to put startup scripts in the /etc/init.d directory. If I move the file to /usr/bin and create a link in /etc/init.d will that work?
    It would help if you let us know the real problem. Why do you need to run manually something that lives in init.d? Those scripts should be run at startup and are not meant to be run manually unless you are doing some administrative task.

    In general adding paths (specially critical system ones like this one) is not a good idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •