If a script should be executed in the current shell, it can be achieved by adding a dot before the command:

   . ./somescript.sh

Is there a way to do this without typing the dot every time? For example a command to change to the parent shell from the script itself?


It may not be exactly what you want but you could do:

alias somescript.sh='. ./somescript.sh'
| improve this answer | |
  • 2
    This solved my problem perfectly. Thank you! – alu Nov 28 '13 at 13:12

Is there a way to do this without typing the dot every time? For example a command to change to the parent shell from the script itself?

I don't think there is (apart from typing source somescript.sh, but that's probably not what you're looking for ^^).

If you run the script as usual with ./somescript.sh, then the shell forks and execs this command. It passes the command to the kernel and then the kernel decides how this command is to be executed by parsing the #! line inside the script. The kernel then launches the specified interpreter (in this case probably a bash subshell). Therefor modifications somescript.sh does to its environment are isolated in the subshell without affecting the parent shell.

If you want the shell to read the script itself without launching another process, you explicitly have to tell it by using the . or source keyword.

| improve this answer | |
  • Thanks for the detailed explanation. :) Now I see that there is no way doing this from the called script, but fortuntly the parent shell can be told not to create a subshell with the alias command, like scott suggested. – alu Nov 28 '13 at 13:20

If you execute ./somescript.sh as an external program, it can't do anything inside the running shell process, such as accessing non-exported variables, setting variables, changing the current directory, etc. There is no way around this.

(Well, ok, you may be able to run a debugger from the subprocess and tweak the memory of the parent process. But if you try it, the most likely result is crashing the parent shell.)

You need to run . ./somescript.sh. You can hide that behind an alias or function if you like, but again the alias or function definition has to be done in the parent shell.

| improve this answer | |
export PATH=$PATH:.

Running that command in the shell or adding it to your shell configuration file will append the current directory to the PATH variable. The PATH variable is a list of directories where your shell will look for binaries to execute when you issue a command.

This will allow you to run any executable in the current directory without ./

| improve this answer | |
  • 3
    The OP is asking about the first . not the ./ that is part of the filename. Running ./somescript does not execute it in the current shell as . ./somescript does. – Anthon Mar 5 '14 at 7:00

I think the script you're trying to run isn't executable. chmod a+x somescript.sh will add the execute bit for user, group, and other (ugo) to that file. The first line of the file will also need the #! line at the top, like #!/bin/bash.

ls -l somescript.sh will show you the rwx triplets (read/write/execute) for the file. It'll need an x in at least the first set, assuming you own the file.

| improve this answer | |
  • The scripts itself works fine, but I want to execute it in the current shell to be able to modify environment variables of the shell. – alu Nov 27 '13 at 23:33
  • If exporting the variable you want in the script isn't working, like export PATH=$PATH:/usr/local/bin to add /usr/local/bin to your $PATH, then you do need to source the file to change your running environment variables. This question is really "How do I programmatically change my environment variables in my running shell from a bash script?" The script . ./somescript.sh is executing just fine -- you're using the dot command to source in a file. tcsh uses "source" for the same thing. – A. R. Diederich Nov 27 '13 at 23:51
  • Modifying a variable was just an example. My question is whether it's possible to tell the shell there should be a dot without actually writing it (for example from the script itself) – alu Nov 27 '13 at 23:58
  • The dot might be easily forgotten which can change the behavior of the script completely. – alu Nov 27 '13 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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