0

For some reason (?), often when I write scripts nowadays they do not work, or work only in part, and then I try with . or source and they work perfectly. I'm unsure what is causing this as the scripts are different in a lot of ways, it's hard to isolate what must be sourced in order for the script as a whole to work. Also, I've noticed that this is almost always the case when I move things from .bashrc aliases and functions into scripts.

But to my actual question, in the above situation, what is the optimal way to "swallow" the source dot, so you are still able to use the scripts as one-word commands, not having to hit the dot every time?

jw013
  • 51,212
Emanuel Berg
  • 6,903
  • 8
  • 44
  • 65
  • OK, good point, let's see - let me put it like this: what is the best way to make a command that will source the script? By command I mean write one word and hit Enter. – Emanuel Berg Sep 10 '12 at 20:14
  • 2
    Sourcing a script and running a script are functionally different. Cosmetic reasons like not liking the . are NOT valid reasons to choose one over the other, and conflating the syntax will probably break many things. Once you understand the differences between functions, scripts, and sourced scripts, come back and describe what you are really trying to do. – jw013 Sep 10 '12 at 20:16
  • 3
    The answer is you can't. Just use two words: . is not that hard to type. Or, make a function: script () { . script; } and use that instead. – jw013 Sep 10 '12 at 20:17
  • It is not about hard or easy, it is about me and my system, and I'd like the interface to be in a certain way. I have so many scripts, alias, functions, you name it, I don't want to memorize all the details and conclude - should there be a dot or not? - I just want to hit the command. If this is contrary to Unix orthodoxy I have no problem with that. – Emanuel Berg Sep 10 '12 at 20:22
  • 2
    Read the question that jw013 linked to and its answers (http://unix.stackexchange.com/a/30964). It looks like you're using a script where you should be using a function. If you're having trouble about one particular case, post its code. Other than that, your question can only have very general answers which have already been given on that previous question. – Gilles 'SO- stop being evil' Sep 10 '12 at 23:27
  • @Gilles: I prefer scripts over functions as 1) files are separated, and I can track them with type; and 2) (most important) if I edit a script and run it, it's there, I don't have to remember sourcing .bashrc (or whatever file). Perhaps that's another question, but if you know how to do that for functions, I'm convinced. As for the link, that's an awesome thread, but it is more about doing it right from day one (or from now on), this (my question) is more fixing a situation that has occurred. – Emanuel Berg Sep 11 '12 at 01:28
  • 2
    But, as you can see, scripts and functions have different roles: scripts live an independent life (and can be written in any language), functions operate inside the shell. It seems that you want to load functions from files; that's a feature bash doesn't have, but zsh does. – Gilles 'SO- stop being evil' Sep 11 '12 at 07:11
  • @Gilles: Well, you could put functions in files and source them in .bashrc - if you put all (and any new) such "function scripts" in a single directory, you don't even have to mention them explicitly in .bashrc. But, the other drawbacks I mentioned are still there - no tracking (at least not with type) and you have to source .bashrc all over after update. (Actually, I'm very happy with jw013's solution.) I'm sure zsh is great, only I'd like to stick with the most common Unix-world choices as to remain mobile myself. – Emanuel Berg Sep 13 '12 at 02:08

2 Answers2

2

If you want a command called script that actually sources the script file instead of running it as a separate process, then make a function:

script () { . /path/to/script; }

To make that function permanent, add it to the relevant rc file for your shell (e.g. ~/.bashrc for bash).

jw013
  • 51,212
  • Check out my comment to Gilles above! Ironically (because you didn't like this at all), this solves all those issues - even the type thing, if you include the full path to the script. (Better move it away from PATH so not to get confused.) To anyone reading this, though, I recommend reading the article that was referred to above :) – Emanuel Berg Sep 11 '12 at 01:41
1

An actual example (or a decomposition of what "doesn't work" into an example) would be best.

That said, when you source a file, you are executing its contents within the same environment (shell) as the one which invokes the operation. sourceing is a good technique for being able to include variables in any number of scripts.

If the script you want to run isn't in your PATH, then you reference your current path (in order to execute the script) by typing "dot", "slash", "filename". This is entirely different than 'source'ing.

JRFerguson
  • 14,740