Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

Example: I have file ch_dir contains following:

#!/bin/bash
cd /usr

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

pandya@pandya-desktop:~$ ./ch_dir
pandya@pandya-desktop:~$

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
    
possible duplicate of Pass variables to script which called script –  muru Aug 24 at 13:10
    
@muru Questions are not same (it seems both answer using source )! –  Pandya Aug 24 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 at 13:16
    
@muru yes but source is not limited to passing variable only. –  Pandya Aug 24 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 at 13:22

2 Answers 2

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
pandya@pandya-desktop:/usr$ 

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
4  
A common shorthand for source is ., as in @mehdy answer. –  Rmano Aug 24 at 18:58
1  
@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 at 20:40

Just run the script with a . like this:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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