Sunday, May 5, 2024
16
rated 0 times [  16] [ 0]  / answers: 1 / hits: 16247  / 3 Years ago, sat, november 20, 2021, 8:25:41

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


More From » command-line

 Answers
3

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.


[#23502] Saturday, November 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eeperant

Total Points: 39
Total Questions: 106
Total Answers: 117

Location: Finland
Member since Sat, Dec 5, 2020
3 Years ago
;