How does calling commands in shell work
No, it is not a command. The way shells work is when you type in a line of text, first word is going to be treated as command, and if the command is not one of the shell built-in ones then the shell will look at all locations listed in PATH
environment variable.
What happens if when the command you want to run is in the same directory as you currently located but that directory isn't on the list of PATH
directories ? That's when you need to use ./
. It is in a way exactly the same as doing /bin/bash
- you are telling the shell where your desired command located, a full path to it. And in case of ./ you're saying to shell "look in this directory". So important part is that you have to be in the same directory where the file is located.
Of course, in order to actually run an executable, it must have executable bit set, so you'll need to chmod +x ./my_file
.
So the important steps:
cd
where you saved the file; if it's in ~/Downloads
, then cd ~/Downloads
- Run
chmod +x ./truecrypt-7.2-setup-x86
, this says "make file truecrypt-7.2-setup-x86 that is in this directory executable"
- And now do
sudo ./truecrypt-7.2-setup-x86
Note that use of ./
is not random behavior, but actually is a standard, specified by Portable Operating System Interface standard ( aka POSIX ), specifically see "Command Search and Execution" section.
Reproducing the error
$ # my script is in ~/Downloads folder
$ stat -c "%n" /home/xieerqi/Downloads/my_script.sh
/home/xieerqi/Downloads/my_script.sh
$ # if I run sudo ./my_script.sh, we get an error
$ sudo ./my_script.sh
[sudo] password for xieerqi:
sudo: ./my_script.sh: command not found
$ # of course the command not found because file is not in ./, not in this dir
$ # this is not sudo's problem
$ # but sudo does indeed show the same error even if you're in same directory
$ cd ./Downloads/
$ sudo ./my_script.sh
[sudo] password for xieerqi:
sudo: ./my_script.sh: command not found
NOTE: the error message given by sudo
is obviously misleading so this is something to be kept in mind; however please note that this wasn't the core of the question that OP is asking.
Documentation and references
From bash
4.3 manual, "COMMAND EXECUTION" section:
If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name.
From Why do you need ./ (dot-slash) before script name to run it in bash?:
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.
./
part of the command is saying "Look in the current directory, and execute the command 'truecrypt-7.2-setup-x86' from here". You need to run this command from the directory where you unpacked the file.chmod +x
, thechmod -x
is the opposite - it removes executable permissions