Source shell script automatically in terminal

14,875

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:
14,875

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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.

  • Admin
    Admin about 9 years
    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
  • Admin
    Admin about 9 years
    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?
  • terdon
    terdon about 9 years
    @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.
  • Jeff Schaller
    Jeff Schaller over 7 years
    a tcsh header line in a .bash_profile?