Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How can I automatically source a particular bash script when I open a terminal window by right clicking somewhere and choosing "open in terminal"?

For example, every time I open a terminal I need to run the following command line:

source myscript

How can I make it so that I don't have to type this automatically?

My script is written in tsch:

#!/bin/tcsh
setenv DISPLAY 127.0.0.1:10.0
cd /ast/dcm/data

I'm using CentOS 7.

share|improve this question
    
Every time you open a terminal window or every time you open one by choosing "open in terminal"? The two are probably not the same. What does "open in terminal" do? What file manager are you using? What desktop environment? Is this something you do to open directories or to show the contents of files or to run scripts or what? Also, what is your default shell? Your script is tcsh, is that what you use in general? –  terdon Apr 3 at 15:45
    
@terdon. Thank you. If I right click on the desktop for example and my script on the desktop I need everytime to source my script. My question is: Can I source this script automatically when I open the terminal window from the desktop? –  goro Apr 3 at 15:58
    
Yes, probably, but to tell you how, I'll need the answers to my previous questions. The simplest approach would be to add source ~/myscript to ~/.bashrc now that I see you've changed it to bash. I'm just not sure what happens when you use this "open in terminal" option. I don't use GUI file managers and you haven't told us which one you are running so it's hard to know. –  terdon Apr 3 at 16:00
    
I am using File explorer as a file manager and I changed it to bash because it was a mistake. I am sourcing bash script. My desktop env. is Genome –  goro Apr 3 at 16:03
3  
That's a tcsh script but you talk about bash. Are you running bash or tcsh? –  Gilles Apr 3 at 22:08

1 Answer 1

up vote 5 down vote accepted

I'm not entirely sure how this works with your file manager but, presumably, "open in terminal" is something you use on directories and it just opens a terminal window at that location. If so, it should be enough to source your script from the initialization file for interactive, non-login shells. If you are using bash, that is ~/.bashrc and you need to edit that file and add this line to it:

. ~/myscript

That assumes that myscript is in your ~/. Now, each time a new shell is started, including when a new terminal is opened, that file will be sourced.


Note, however, that the script you show is not a bash script. There is no setenv command in bash, that's a C-shell thing. The bash equivalent would be:

#!/bin/bash
export DISPLAY=127.0.0.1:10.0
cd /ast/dcm/data
share|improve this answer
    
Thank you very much for your help. Kindly do you mean that my script sould be like this #!/bin/bash . ~/myscript setenv DISPLAY 127.0.0.1:10.0 cd /ast/dcm/data –  goro Apr 3 at 16:23
    
@MJA I don't understand what you mean. Your script should be exactly as it is. Just put the . /path/to/script.sh command in your ~/.bashrc. –  terdon Apr 3 at 16:25
    
Also in terminal ===> edit menu ===> profiles preferences ===> title an command tab ===> run a custom command instead of my shel ===> I tried to type source myscript but it did not work? –  goro Apr 3 at 16:26
    
@MJA you need to add that line to the ~/.bashrc file. Come visit the Unix & Linux Chat room if it's still giving you trouble. –  terdon Apr 3 at 16:27

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.