5

How can I automatically source a particular shell 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.

  • 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 '15 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? – user88036 Apr 3 '15 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 '15 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 – user88036 Apr 3 '15 at 16:03
  • 3
    That's a tcsh script but you talk about bash. Are you running bash or tcsh? – Gilles Apr 3 '15 at 22:08
5

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
  • 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 – user88036 Apr 3 '15 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 '15 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? – user88036 Apr 3 '15 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 '15 at 16:27
-1

In your home Directory :
run ls -a -l

  • You will see the complete directory structure including the hidden files
  • check if there is any file named .bash_profile
  • if no .bash_profile

    • create a .bash_profile (touch .bash_profile)
  • vi .bash_profile

  • Insert your script in the .bash_profile--

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

If you script is right ,when ever you open your terminal and is in the path, where you have created the .bash_profile ,your script will run automatically.

Note:This is LINUX and CentOS

  • 1
    a tcsh header line in a .bash_profile? – Jeff Schaller Sep 27 '16 at 18:31

Your Answer

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