Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I want to executed command from file (which is script) in current shell in terminal.

Example: I have file ch_dir contains following:

cd /usr

Now if I execute file usually as following then it executes commands in different shell:

pandya@pandya-desktop:~$ ./ch_dir

Here cd /user is executed in different shell.

But I want to execute in Current shell. How do I do that?

Note: Here cd is only used to explain. Question's aim is to execute any command in current shell

share|improve this question

marked as duplicate by muru command-line May 20 at 8:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@muru Questions are not same (it seems both answer using source )! – Pandya Aug 24 '14 at 13:15
Questions are same, different motivations: How do I affect the calling shell from a script/file? Because of all the context, the noise added makes it unclear. – muru Aug 24 '14 at 13:16
@muru yes but source is not limited to passing variable only. – Pandya Aug 24 '14 at 13:19
That's just the example used. The general question, which neither of these questions have stated, is "How do I affect the calling shell from a script?" If you edit this question to reflect that I'd be happy to retract this close vote and flag the other. To me, both are dupes. Both of you are asking two specific forms of the same general problem. – muru Aug 24 '14 at 13:22
up vote 12 down vote accepted

Use builtin command source (from help -m source):

source - Execute commands from a file in the current shell.

Example of execution mentioned in Question:

pandya@pandya-desktop:~$ source ch_dir

Here you can see cd /usr is executed in current shell and hence directory of current shell changed!

Hope this will help to execute command input from file in current shell.

share|improve this answer
A common shorthand for source is ., as in @mehdy answer. – Rmano Aug 24 '14 at 18:58
@Rmano . also works in more Bourne-style shells than source, which is provided specially by bash. For example, dash (Ubuntu's default system sh) doesn't have source. (On the other hand, source works in some non-Bourne-style shells that don't use ., like csh / tcsh.) – Eliah Kagan Aug 24 '14 at 20:40

Just run the script with a . like this:

pandya@pandya-desktop:~$ . ./ch_dir
share|improve this answer

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