Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions Tips and Tutorials The Whole Story on #! /usr/bin/ksh Post 302112878 by Perderabo on Saturday 31st of March 2007 09:14:34 PM
Old 03-31-2007
Part 2 -- The Details

The Format of the #! Line

We can say for certain that the first 2 characters must be "#!". Or can we? Many systems, it seems, are willing to delete leading white space. My recommendation is to start with #!. It's traditional.

Next there may be an optional space. Some documentation says this space is required but as far as anyone can determine the only Unix release to require the space was a snapshot release of BSD 4.1... this was not a general release). Actually, it appears that you may have several spaces if you want. And some testing with TAB characters has been done and seems to work. My recommendation is to stay with zero or one spaces.

Next comes the full path to the interpreter and like all full paths, it must start with a /. Oops, another exception... The Linux kernel (at least version 2.0.34) is willing to accept a relative path. My recommendation is don't do that.

We may be done. Or we may have optional white space which lead to our single argument. Except that some versions of FreeBSD handle multiple arguments.

Most versions of BSD and HP-UX will strip trailing white space. Other versions of Unix treat trailing white space as valid characters. And a few versions of BSD can accept a trailing comment delimited by a # character.

How long can the line be? A few versions of Unix set the limit as low as 32 characters. FreeBSD can apparently handle 8192 characters.

At least the line always ends with the Unix standard \n character, right? Well, not always. Some versions of Unix will tolerate a \r\n ending and strip off the \r while others won't do that.

This is not as standard as it could be...

Argument 0 of The Process, Not The Script

There is another way that implementations may differ. Consider the perl script that I ran ar the end of part 1. My shell did the approximate equivalent of
execl("./perlargs", "./perlargs", "one", "two", "three", (char *) NULL)
and the kernel transformed it into the approximate equivalent of
execl("/usr/local/bin/perl", "/usr/local/bin/perl", "-w", "./perlargs", "one", "two", "three", (char *) NULL)

Highlighted in red is argument zero which by convention is the same as the path of the program being executed. A notable execution is that the login program will set it to stuff like "-ksh". Originally, executable shell scripts had the argument 0 set to the name of the script rather than the name of the interpreter. These days, the name of the interpreter is common. The last hold-out I know of is HP-UX which sets argument 0 to the name of the script.
These 3 Users Gave Thanks to Perderabo For This Post:
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

#!/usr/bin/ksh Command Interpreter in a sh script

Hi, I have a developer that is trying to start a script with sh "scriptname". In the script, he is specifying #!/usr/bin/ksh as the command interpreter. For some reason sh is ignoring the #!/usr/bin/ksh. We are running Solaris 8. Does anyone have any ideas what could be causing this? Here... (3 Replies)
Discussion started by: ckeith79
3 Replies

2. UNIX for Dummies Questions & Answers

ksh: /usr/bin/ls: arg list too long

I am using IBM AIX unix version 4.3.3.0. In a directory there are many files with different patterns. When I am trying to execute the command, ls -l with the file pattern, which have fewer files it gives the desired result. However when I am trying to execute the same command for file pattern,... (2 Replies)
Discussion started by: jitindrabappa
2 Replies

3. Solaris

How do I link ld in /usr/ucb/ to /usr/ccs/bin?

Hi all, below is the problem details: ora10g@CNORACLE1>which ld /usr/ucb/ld ora10g@CNORACLE1>cd /usr/ccs/bin ora10g@CNORACLE1>ln -s /usr/ucb/ld ld ln: cannot create ld: File exists ora10g@CNORACLE1> how to link it to /usr/ccs/bin? (6 Replies)
Discussion started by: SmartAntz
6 Replies

4. Shell Programming and Scripting

!/usr/bin/ksh error

Usually we use !/usr/bin/ksh at the start of the script.But if I am having this stuff in the scripts and calling one script from other its not working.What may be the reason behind it ? xyz.ksh #!/usr/bin/ksh echo "Hi" abc.ksh #!/usr/bin/ksh echo "I am fine" ksh xyz.ksh Its... (4 Replies)
Discussion started by: dr46014
4 Replies

5. AIX

aix:ksh: /usr/bin/rm: 0403-027 The parameter list is too long.

Hi, I am getting the below error message When i am trying to delete the files from the directory.Could you please guide me? rm *.aud ksh: /usr/bin/rm: 0403-027 The parameter list is too long. and find /oracle/admin/testP/adump/*.aud -mtime +5 -exec rm {} \; ksh: /usr/bin/find:... (3 Replies)
Discussion started by: nokiae63
3 Replies

6. OS X (Apple)

When to use /Users/m/bin instead of /usr/local/bin (& whats the diff?)?

Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself. But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies

7. Shell Programming and Scripting

/usr/bin/ksh -E

I saw one script using the first line as below /usr/bin/ksh -E I have used -x for debug but couldn't find what is this -E option for ? Pls let me know what is this -E used for Thanks RL (1 Reply)
Discussion started by: reldb
1 Replies

8. BSD

FreeBSD: /usr/bin/ld not looking in /usr/local/lib

I'm not sure if this is the default behavior for the ld command, but it does not seem to be looking in /usr/local/lib for shared libraries. I was trying to compile the latest version of Kanatest from svn. The autorgen.sh script seems to exit without too much trouble: $ ./autogen.sh checking... (2 Replies)
Discussion started by: AntumDeluge
2 Replies
ATF-SH(1)						    BSD General Commands Manual 						 ATF-SH(1)

NAME
atf-sh [-s shell] -- interpreter for shell-based test programs SYNOPSIS
atf-sh script DESCRIPTION
atf-sh is an interpreter that runs the test program given in script after loading the atf-sh(3) library. atf-sh is not a real interpreter though: it is just a wrapper around the system-wide shell defined by ATF_SHELL. atf-sh executes the inter- preter, loads the atf-sh(3) library and then runs the script. You must consider atf-sh to be a POSIX shell by default and thus should not use any non-standard extensions. The following options are available: -s shell Specifies the shell to use instead of the value provided by ATF_SHELL. ENVIRONMENT
ATF_LIBEXECDIR Overrides the builtin directory where atf-sh is located. Should not be overridden other than for testing purposes. ATF_PKGDATADIR Overrides the builtin directory where libatf-sh.subr is located. Should not be overridden other than for testing purposes. ATF_SHELL Path to the system shell to be used in the generated scripts. Scripts must not rely on this variable being set to select a specific interpreter. EXAMPLES
Scripts using atf-sh(3) should start with: #! /usr/bin/env atf-sh Alternatively, if you want to explicitly choose a shell interpreter, you cannot rely on env(1) to find atf-sh. Instead, you have to hardcode the path to atf-sh in the script and then use the -s option afterwards as a single parameter: #! /path/to/bin/atf-sh -s/bin/bash ENVIRONMENT
ATF_SHELL Path to the system shell to be used in the generated scripts. SEE ALSO
atf-sh(3) BSD
September 27, 2014 BSD
All times are GMT -4. The time now is 10:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy