Running scripts without dot slash (Ubuntu vs Mac)

2019-02-26 06:54发布

In ubuntu scripts can be executed with following commands:

$ chmod +x manage.py
$ manage.py

However in mac you need to use ./ in order to actually run the script, as follow:

$ chmod +x manage.py
$ ./manage.py

I would like to know what is exactly ./ (especially that both system use bash by default) and if there is a way to run scripts directly in mac?

标签: shell unix
2条回答
叛逆
2楼-- · 2019-02-26 07:26

It's because you (very sensibly) don't have . in your PATH environment variable. If you do, it becomes an attack vector for people to get you to execute their own code instead of real stuff.

For example, let's say your path is:

.:/usr/bin

so that commands will first be searched for in your current directory, then in /usr/bin.

Then another user creates an executable script file ls in their home directory which changes to your home directory and deletes all your files. Then they tell you they've got something interesting in their home directory. You run ls to see what they have, and your files are deleted. All because it ran ls from your current directory first.

This is a particular favorite attack vector against naive system admins.


To be honest, on my home machines, I don't worry too much, since I'm the only user and I'm not prone to downloading stuff I don't trust. So I usually add . to my path for convenience, but usually at the end so it doesn't get in the way of my more regular commands.

查看更多
你好瞎i
3楼-- · 2019-02-26 07:35

When you are executing a command that file (script/binary) needs to be found by the system. That is done by putting directories where to look for scripts into the PATH environment variable. So if it works in ubuntu it means PATH includes '.' (the current directory). If you want the same behavior on mac then put something like export PATH="$PATH:." in your .bashrc (asuming you are using bash..)

查看更多
登录 后发表回答