Why do I need to type `./` before executing a program in the current directory?

1

Why do you need to type ./ before executing a program?

In the terminal, whenever you type the name of an application, let's say gedit, the terminal will go look in some (pre-defined) directories that contain applications (the binaries of the applications). The names of these directories are contained in a variable called PATH. You can see what's in this variable by executing echo $PATH. See those directories separated by :? Those are the directories that the terminal will go search in, if you just type gedit, nautilus, or a.out. As you can see, the path of your a.out program is not there. When you do ./a.out, you're telling the terminal "look in the current directory, and run a.out, and don't go look in PATH.

Solution 1

If you don't want to type ./ every time, you'll need to add a.out's directory in $PATH. In the following instructions, I'll assume that the path to a.out is /path/to/programs/, but you should change it to your actual path.

Simply add...

0 0
2

Because on Unix, usually, the current directory is not in $PATH.

When you type a command the shell looks up a list of directories, as specified by the PATH variable. The current directory is not in that list.

The reason for not having the current directory on that list is security.

Let's say you're root and go into another user's directory and type sl instead of ls. If the current directory is in PATH, the shell will try to execute the sl program in that directory (since there is no other sl program). That sl program might be malicious.

It works with ./ because POSIX specifies that a command name that contain a / will be used as a filename directly, suppressing a search in $PATH. You could have used full path for the exact same effect, but ./ is shorter and easier to write.

EDIT

That sl part was just an example. The directories in PATH are searched sequentially and when a match is made that program is executed. So, depending on how PATH...

0 0
3

When running scripts in bash, I have to write ./ in the beginning:

$ ./manage.py syncdb

If I don't, I get an error message:

$ manage.py syncdb -bash: manage.py: command not found

What is the reason for this? I thought . is an alias for current folder, and therefore these two calls should be equivalent.

I also don't understand why I don't need ./ when running applications, such as:

user:/home/user$ cd /usr/bin user:/usr/bin$ git

(which runs without ./)


ANSWERS:

Because on Unix, usually, the current directory is not in $PATH.

When you type a command the shell looks up a list of directories, as specified by the PATH variable. The current directory is not in that list.

The reason for not having the current directory on that list is security.

Let's say you're root and go into another user's directory and type sl instead of ls. If the current directory is in PATH, the shell will try to execute the sl program in that...

0 0
4

This document is intended as a way of answering some of the questions that are frequently asked (especially by beginners) about Unix on OS X. The answers have been updated for Leopard (OS X version 10.5). Feel free to suggest additions or corrections via the email address given at the bottom of the page.


This FAQ was originally hosted on the MacOSXHints forums site and was sparked by questions asked there and suggestions by forum members.

The Unix

command-line

provides an alternative to the

GUI

provided by OS X.

Most Macintosh users will not need to use the command-line but it provides a more efficient way of accomplishing some tasks (especially repetitive ones). The command-line is also an essential troubleshooting tool in some situations.

Terminal
You get access to the command-line by launching the application called "Terminal" which is in the "/Applications/Utilities" folder.
When you open a new Terminal window, it will show...

0 0
5

Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 8.2
Primary Software Fixed Version: N/A
Secondary Software: N/A


Problem:

I'm using the Current VI's Path constant and stripping off the VI name in order to access the owning directory. This works in the development environment, but when I build my VI into an executable I am having some problems, including, but not limited to receiving the error below when performing File I/O.

Error 7 occurred at New File.

Why is this happening? How do I build my VI such that I do not have to make any changes when I move from the development environment to a stand-alone application?

Solution:
The Current VI's Path node returns the full path including the VI's name. This path varies based on whether the VI is running in the LabVIEW development environment or in a stand-alone application. LabVIEW wraps all VIs associated with a...

0 0
6


Well spoken...

You CAN set up a Linux system to be incomprehensible to anyone but you.

If you do this, you SHOULD not expect mere mortals to help you

You don't have to use ./. Just add it to your PATH variable. Probably won't be too risky for the unprivileged user, just wouldn't recommend adding ./ to the privileged user's PATH. It's really not much different than DOS/Windows? There's a %PATH% variable in the Microsoft world as well it just includes ./ (if you will) by default.

It's a convention that has been used for 30 odd years for security reasons. Make sure you understand the potential consequences before you add it. That's the thing about security...it's a pain in the a** and if it works you'll never even know it. If you choose not to use it, it'll be too late before you know it. Keep in mind that spyware, adware, malware, and virii don't ask permission before infecting a Windows machine precisely because MS users generally choose...

0 0
7

I've asked this before and the answer I got was basically "What if a bad program
executed without admin rights, and littered the drive with another bad program
that had the same name as a system command (like DIR)? Then when an admin was in
that directory and typed the DIR command the bad program would run with admin
rights."

I agree that it is possible, but it rarely happens and anti-virus should catch
it and I'm getting tired of doing more work and extra steps for these what if
scenarios. What if a bad guy breaks a window and comes into my house? Does
that mean I should replace all my windows with bricks? Well maybe, but that's a
choice I get to make, a balance between security and convenience/enjoyment.
PowerShell gives me no choice between risk and productivity here.

Microsoft has been beat up so hard about security (and most of it was justified)
that now they're taking it too far and actually hurting productivity....

0 0
8

Commenter Dean Earley asks, "Why is there a 'current directory' AND an current drive? Why not merge them?"

Pithy answer: Originally, each drive had its own current directory, but now they don't, but it looks like they do.

Okay, let's unwrap that sentence. You actually know enough to answer the question yourself; you just have to put the pieces together.

Set the wayback machine to DOS 1.0. Each volume was represented by a drive letter. There were no subdirectories. This behavior was carried forward from CP/M.

Programs from the DOS 1.0 era didn't understand subdirectories; they referred to files by just drive letter and file name, for example, B:PROGRAM.LST. Let's fire up the assembler (compilers were for rich people) and assemble a program whose source code is on the A drive, but sending the output to the B drive.

Since we gave only a drive letter in response to the Object file prompt, the assembler defaults to a file name of FOO.OBJ,...

0 0
9
...
0 0
10

Environment variables are system (or user) wide settings that programs and Windows can use to control behaviour. Particularly relevant to working with the command line are the PATH and PATHEXT environment variables.

The PATH environment variable contains a list of paths that Windows searches when you enter a command. If you don't specify an explicit path to a command (a full path looks something like C:\Python25\python.exe), windows first looks in the current directory and then it checks all the directories specified in the path. Only if the command can't be found in any of these directories does the console return the 'something' is not recognized as an internal or external command... message.

The most important thing to check is that Python is on your path. I think that the Windows installer for Python puts it on the path, but I have worked on systems where this is not the case.

Open the command console and type python. If Python is on your path you will see...

0 0
11

Install---then Compiling and Execute your first C# Program

When I first heard about Microsoft's newest language, C#, my mouth started watering. For the audience with which I mostly deal (beginner programmers trying to break into the business) I thought C# would be the great equalizer---after all, everyone (experienced programmers as well as beginners) would be learning it at the same time.

Many of you may know I'm in the process of writing a C# book--this article will detail how to install it, and how to write your first program. For more, you'll need to read my book!

Installing C#

To install C#, for the moment anyway, you don't need to purchase a thing. You just need to get your hands on the Microsoft .Net Framework.

C# is a language that comes with the Microsoft .Net (pronounced DOT-NET) Framework. The .Net Framework is an environment that permits .Net programs to run. C# is just one of many programming languages supported by the .Net framework (it...

0 0