1

I thought it would be easy to find an answer to this question, but I can find nothing on the web:

I have a function declared in .bashrc. I want to call that function from within a script. If I type:

$> source scriptname

the function is found. If I try:

$> scriptname

the function is not found.

At this point I can't see any reason why bash wants to make this difficult. Is there some way to make the 'source'ing automatic?

Ray Andrews
  • 532
  • 4
  • 14
  • Is there are reason for not using `source`? It exists as a means of importing the contents of one script into another. – D.Shawley Nov 19 '11 at 19:45
  • Ksh93+ has the FPATH variable. Maybe bash 4 has added that. See http://www2.research.att.com/sw/download/man/man1/ksh.html for info on FPATH. Good luck. – shellter Nov 19 '11 at 19:50
  • accept answers on your questions so people know they are resolved. – Karoly Horvath Nov 21 '11 at 10:06

2 Answers2

0

If you need to call it directly by the name, you can write a small shell script for it and place it in some directory in the PATH.

e.g. File scriptname:

#!/bin/bash

echo "Starting `basename $0`"
# Other processing
echo "Completed `basename $0`"

Place it in some directory like /usr/bin, then you'll need to call scriptname from the command line without '.' or 'source'.

Hope this answers your question.

mtk
  • 13,221
  • 16
  • 72
  • 112
0

you can execute with ./scriptname, but that will create a new process, so all the defined/exported variables, aliases and everything will be lost after the script is finished.

Keep using source scriptname or . scriptname.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • The reason I don't want to use '.' or 'source' is just convinience, I'm creating a bunch of 'wrappers' around commonly used commands, just to, in effect, create very quick, simplified versions of them with the defaults I like, and with a very simple and consistent argument list. Since speed is the main point of this, anything that adds to the typing is to be avoided if possible. – Ray Andrews Nov 19 '11 at 20:37
  • So, use the short form -- the dot followed by whitespace followed by your filename! – JRFerguson Nov 19 '11 at 22:27
  • I don't understand your "speed" "argument".. are you saying that because you have to type `source` it's going to be slower? that's not the case. – Karoly Horvath Nov 20 '11 at 00:08
  • Well, if I hafta, I hafta. But I don't know why bash has to be so difficult about things. Thanks anyway guys. – Ray Andrews Nov 20 '11 at 00:39
  • I don't know *any* language where you can import a source just by typing its name. I don't see difficulty here, and if you do you got the wrong mindset. ah, and pls accept the answer. – Karoly Horvath Nov 20 '11 at 01:00