[opensuse] Re: umm BASH gurus, what does the typo $!/bin/bash do?
David C. Rankin wrote:
Guys,
This was bizarre. Doing a quick script, I accidentally did a $!/bin/bash instead of #!/bin/bash. The script gave no output, but appeared to be struggling much longer than it should (~1 sec) then would exit normally. No error no nothing. Something worked though. I had set a trap to remove a temp dir in it and provide output. When I ssh'ed into another box and came back, evidently the EXIT signal was generated and the trap ran -- even though it the script defined $! instead of #!.
Can anybody tell me, or point me to any info, concerning just what $! did??
Normally, I script that has $!/bin/bash in its first line, should start an interactive bash and wait for further input. I.e., I don't think it exited normally, you just got a same-looking prompt from a subshell. When you then enter "exit" or type Ctrl-D the bash is finished and the rest of this script will be executed. Try it: A basic example: puma:tmp $ cat try.sh $!/bin/bash echo hallo Let's see which process we are in: puma:tmp $ ps -fp $$ UID PID PPID C STIME TTY TIME CMD schrod 6080 6079 0 09:44 pts/8 00:00:00 bash Well, our process id is 6080. Execute the script and check which process we are in now: puma:tmp $ ./try.sh puma:tmp $ ps -fp $$ UID PID PPID C STIME TTY TIME CMD schrod 6190 6189 0 09:46 pts/8 00:00:00 /bin/bash As you'll see, a different process, with process id 6190. (The parent process id 6189 is the try.sh-process that has 6080 as parent.) No exit that sub-shell. puma:tmp $ exit hallo Rest of the script (echo hallo) is executed, as you can see. Are you sure that the trap run? The file was not there -- but was it created in the first place? Your trap statement in the shell script will never be reached and thus the trap should not be set up at all. Technical Background: Concerning #! as the first 2 chars in a script: This is necessary for the execution of this command with the system call exec() (or one of its siblings, like execve()). Then it provides the "magic number" for the loader to handle this "executable". I.e., it is necessary when called from another program or with "exec" from another shell script. It is not necessary when called interactively from a bash session. Thus, the first line was $!/bin/bash. $! expands to empty, so it's just a line /bin/bash. That line causes bash to be executed. Since there are no further arguments, an interactive shell is started. HTH, Joachim -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Joachim Schrod Email: jschrod@acm.org Roedermark, Germany -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (1)
-
Joachim Schrod