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?

closed as not a real question by Gilles, Renan, Stéphane Gimenez, rahmu, Tim Kennedy Sep 26 '12 at 19:08

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
  • 2
    Read the question that jw013 linked to and its answers (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 Sep 10 '12 at 23:27
  • 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 Sep 11 '12 at 7:11

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).

  • 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 1:41

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.

  • Yes, I know this (but thanks). See the above discussion, that will clarify what I want. – Emanuel Berg Sep 10 '12 at 20:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.