jbayer@ispi.COM (Jonathan Bayer) (09/14/90)
I know that this subject comes up about once a year or so. Now it is my
turn :-)
I have a customer who wants/needs the prompt to show the current
directory (similar to MS-DOS :-( ). I reached back into my archives
and pulled out the following script:
c() # you might not be able to name this function `cd'
{
cd $1
PS1="`pwd`> "
}
This was placed into /etc/profile, and works well, until a new shell is
spawned. The new shell does not inherit the shell function. My
questions are:
1. How can I make the new shell inherit shell functions?
2. Is it possible to have a shell function have the same
name as a real function (ie: name this function "cd",
and have it use the normal "cd" to change the
directory)?
I will summarize to the net after stripping out all extraneous lines and
comments.
Thank you.
JB
--
Jonathan Bayer Intelligent Software Products, Inc.
(201) 245-5922 500 Oakwood Ave.
jbayer@ispi.COM Roselle Park, NJ 07204
gt0178a@prism.gatech.EDU (BURNS,JIM) (09/14/90)
in article <1683@ispi.COM>, jbayer@ispi.COM (Jonathan Bayer) says: > I have a customer who wants/needs the prompt to show the current > directory (similar to MS-DOS :-( ). I reached back into my archives > and pulled out the following script: In .kshrc, I do: PS1="{${HOSTNAME}"':${PWD}}\ [!] ' In .cshrc, I do: alias cd 'cd \!*;set prompt="`hostname``pwd`> [\\!] % "' cd `pwd` Putting them in the rc's executes them for each subshell. (The !'s are the current history #'s.) > spawned. The new shell does not inherit the shell function. My > questions are: > > 1. How can I make the new shell inherit shell functions? Ksh allows you to export functions with (for me): typeset -fx cp ll man I'm not aware of any way to do it in sh, and haven't run across any csh's that support functions, per se (not aliases). > 2. Is it possible to have a shell function have the same > name as a real function (ie: name this function "cd", > and have it use the normal "cd" to change the > directory)? Most shells will search for a builtin before executing an alias or function. For a function that replaces the behavior of an external, specify the pathname for the 'inside' invocation of the command, as in: ll () { /bin/ls -alF $* | more ; } This can be particularly important in shells that allow recursion! -- BURNS,JIM Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332 uucp: ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a Internet: gt0178a@prism.gatech.edu
chet@cwns1.CWRU.EDU (Chet Ramey) (09/14/90)
In article <13669@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes: >Ksh allows you to export functions with (for me): > >typeset -fx cp ll man This will not export the functions to separate invocations of ksh. Only shells `spawned' by ksh to execute scripts will inherit these function definitions. There is no way to export functions to separate invocations of the shell; you must do it via the $ENV file. >I'm not aware of any way to do it in sh, and haven't run across any csh's >that support functions, per se (not aliases). There aren't any. >Most shells will search for a builtin before executing an alias or >function. For a function that replaces the behavior of an external, >specify the pathname for the 'inside' invocation of the command, as in: The Korn Shell and Bash will do alias substitution first. It's really the last stage of token recognition rather than part of command execution. For bash, the order of lookup is function, builtin, file system (Posix is the same way). For ksh, it's builtin, function, file system. Chet -- Chet Ramey ``Levi Stubbs' tears run down Network Services Group his face...'' Case Western Reserve University chet@ins.CWRU.Edu
gt0178a@prism.gatech.EDU (BURNS,JIM) (09/16/90)
in article <1990Sep14.135525.8981@usenet.ins.cwru.edu>, chet@cwns1.CWRU.EDU (Chet Ramey) says: > This will not export the functions to separate invocations of ksh. Only > shells `spawned' by ksh to execute scripts will inherit these function > definitions. There is no way to export functions to separate invocations > of the shell; you must do it via the $ENV file. Actually, I do put my functions in .kshrc. I include the typeset also because I find vi is funny about what it includes in a subshell, and wasn't defining my ll() function w/o the typeset. As a matter of fact, most of what is in my .kshrc is there instead of in .profile, not because I want to maintain compatibility w/ /bin/sh, but because of vi's quirks. > The Korn Shell and Bash will do alias substitution first. It's really the > last stage of token recognition rather than part of command execution. For Sorry, of course you're right. I tested his function (calling it cp) to be sure of the evaluation order, but then my mind short circuited when I wrote that builtins executed before aliases as well. :-) -- BURNS,JIM Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332 uucp: ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a Internet: gt0178a@prism.gatech.edu