ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a aquestion about the unix "." (period" command)
as far as i know,that command is used to execute a shell script within the current shell,instead of creating a new subshell.
Now...
When i use that command with any of my shell scripts,it works fine.
however,when i use it with any other executable file,it says "command not found".
That happens even when the programm i'm trying to execute is within my "PATH" directories.
So..
Is this command used ONLY for shell scripts?
If yes,how does the shell distinguish between shell scripts and other executable files?
I'm asking that,because i always use "chmod +x' on my scripts to make them executable...
So...I imagine that the shell has to know if a file is a shell script or not,in order to use or not the "." command"..
1. Yes, the "." is used by many *nix shells to execute the shell script in the current shell rather than creating a subshell). This is the only way, for example, you can read a script that defines variables in your current shell.
2. And yes, the first argument after the "." must be the name of a shell script. It cannot be a binary.
Why?
Because the shell is expecting a text script (not an executable binary) ... and it tries to *read*, and then to *parse* that script. And of course you don't parse a binary: you execute it.
Try it:
- vi hello.c =>
#include <stdio.h>
int
main (int argc, char *argv[])
{
return printf ("Hello world!\n");
}
- cc -o hello hello.c
- ./hello =>
Hello world!
<= SO FAR, SO GOOD. WE'VE GOT A BINARY, AND WE CAN EXECUTE IT.
- od -cx hello =>0000000 177 E L F 001 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
457f 464c 0101 0001 0000 0000 0000 0000
0000020 002 \0 003 \0 001 \0 \0 \0 \0 203 004 \b 4 \0 \0 \0
...
<= YOU'LL NOTICE THAT THE FIRST 4 BYTES OF ANY LINUX EXECUTABLE
ARE THE CHARACTERS "\0x7fELF"
- . ./hello =>
bash: ELF: command not found
<= AND YOU'LL SEE THAT THE "BASH" SHELL TRIED TO PARSE THE
"TEXT" IN THE (BINARY!) FILE "HELLO" ... AND FAILED TO UNDERSTAND IT
Your mileage may vary depending on exactly which shell you're using, and exactly how you type the command. But the bottom line is "Yes, the '.' requires a shell script."
Originally posted by in_touch
what happens when we just type "prompt> ./<some-shell-script"?
obviously our shell forks a new shell to execute the script.
however,how does the shell know how to handle the file?
it might be a a c program or simply a script.
As you rpobably know, the "dot" in "./somescript" is a totally different one from when your do ". somescript".
". somescript" includes the shell-commands in the same shell process, and "./somescript" or "./some_c_program" runs the mentioned program in a new (child-) process of the shell.
". somescript": here the dot is a shell command.
"./somescript": here the dot indicates the directory where the program or script to run resides.
"./somescript": here the shell tries to run (execute) somescript, and if the file "somescript" has execute permissions (the x's you see when you do "ls -l") it will run. The shell check the first bytes, and if they are "#!" it acutally executes the binary executable (ELF file, say "C-program") mentioned in the same line. If the bytes are not "#!" then try to make the system (kernel) execute it directly as a binary executable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.