pykaczka
pykaczka

Reputation: 31

Source in .sh script does not work after opening new terminal from .sh script

I have a problem with sourcing aliases. It's really specific case. I open new terminal window with two tabs using open_new.sh:

    #!/bin/bash
    gnome-terminal --tab --title="Tab1" -e "./tab1.sh" --tab --title="Tab2" -e "./tab2.sh"

In file tab1.sh i have:

    #!/bin/bash
    ls
    . ~/.my_aliases
    echo "done"
    exec bash

File tab2.sh looks very similar. Line 2 and 4 works fine but it looks like line 3 does not work. When I try to use 'lll' alias it says 'bash: lll: command not found'. Then when I type command from line 3 directly in terminal aliases start working.

I have tried many different solution but I still can't handle it. For example I've tried something like this before sourcing:

    shopt -s expand_aliases

I've tried this:

    source ~/.my_aliases

And also I've tried:

    #!/bin/bash -i

Is someone able to help me? Maybe I should mention two things: I am not root user, I don't have any problems with sourcing that file with aliases in normal way.

Thanks in advance.

Upvotes: 1

Views: 1084

Answers (2)

pykaczka
pykaczka

Reputation: 31

I've been looking for answer for three days before I asked question here. And now I've found the answer that I want to share if someone else will struggle with this problem in the future. In file open_new.sh:

#!/bin/bash
gnome-terminal --tab --title="Tab1" -e "bash --rcfile ./tab1.sh" --tab --title="Tab2" -e "bash --rcfile ./tab2.sh"

In file tab1.sh:

. ~/.bashrc
. ~/.my_aliases
ls
echo "done"

Explanation:

--rcfile File #execute commands from File instead of the standard personal initialization file ~/.bashrc

In File you can place many other commands. All bash commands worked for me. I've found solution in:

man bash

Upvotes: 1

Liviu Chircu
Liviu Chircu

Reputation: 1039

I suggest appending the following to your ~/.bashrc:

source ~/.my_aliases

This way, all new tabs will inherit your aliases.

Upvotes: 0

Related Questions