0

Is there a way to source some bash script when I run command mc? I want to have it sourced in the bash instance of midnight commander. I tried something like this

mc && . .bash_script.sh 

and it doesn't work. It seems like Bash sourced the script in previous Bash instance because after running exit command I see that .bash_script.sh was sourced. But in midning commander's command line it's not available.

  • can you give us an idea as to what the script is actually doing? – schaiba Sep 1 '16 at 8:02
  • It starts bash git-prompt github.com/magicmonty/bash-git-prompt. I have this piece of code in .bash_login. if [ -f ~/.bash-git-prompt/gitprompt.sh ]; then . ~/.bash-git-prompt/gitprompt.sh fi – D3ryk Sep 1 '16 at 8:24
1

man mc, search for "bashrc" keyword:

If you are using bash you can specify startup commands for the subshell in your ~/.local/share/mc/bashrc file and special keyboard maps in the ~/.local/share/mc/inputrc file. tcsh users may specify startup commands in the ~/.local/share/mc/tcshrc file.

So, assume ~/.bash_script.sh is the path of your .bash_script.sh, add ~/.local/share/mc/bashrc file with this content:

[xiaobai@localhost tmp]$ cat ~/.local/share/mc/bashrc
. ~/.bash_script.sh
[xiaobai@localhost tmp]$ 

After issue mc (or mc -U), the bottom command prompt will change to current time.

Then press Ctrl-o to go inside subshell, the gray color+git info will take effect only after you issue first command here (bug). Press Ctrl-o again to go back mc main screen.

  • It worked to add my code into ~/.local/share/mc/bashrc. Thx for your suggestion. Bad thing is that gitprompt doesn't work. However it was sourced from . ~/.bash-git-prompt/gitprompt.sh;. But prompt in shell is gray(not colorized) and it doesn't contain information about changes in repo. :( – D3ryk Sep 2 '16 at 6:41
  • @user5587353 You need Ctrl-o go inside subshell and issue a at least ONE command(e.g. ls) to see the effect, and this effect only apply inside subshell session, not mc main screen. – 林果皞 Sep 2 '16 at 7:10
  • I see, prompt is collorized after pressing eg. enter. But It doesn't contains information about changes in repo like [ master | ✔ ]. That's what github.com/magicmonty/bash-git-prompt doing. – D3ryk Sep 2 '16 at 7:18
  • @user5587353 Are you sure you already cd to git project directory ? I have no problem to see ✔ ~/git/android/xxx [master|✚ 5] – 林果皞 Sep 2 '16 at 8:00
0

mc runs ~/.bashrc. So, we can run your .bash_script.sh from within ~/.bashrc. It sounds like you only want to source the script if you are inside mc. In that case, test for a shell variable that mc sets, for example: MC_SID.

So, in your ~/.bashrc add the line:

[ "$MC_SID" ] && . .bash_script.sh 

Testing

To verify that this method works, start with something simple. Put the following line in ~/.bashrc:

[ "$MC_SID" ] && date >>~/mc.log

Next, start an instance of mc. Then, run ls ~/mc.log to see if this file exists.

  • I tried proposed solution but it doesn't work. I have add this piece of code [ "$MC_SID" ] && [ -f ~/.bash-git-prompt/gitprompt.sh ] && { GIT_PROMPT_ONLY_IN_REPO=1; GIT_PROMPT_THEME=Single_line_Ubuntu; . ~/.bash-git-prompt/gitprompt.sh; } – D3ryk Sep 1 '16 at 9:01
  • @user5587353 For a test case, that is a bit too complex. Let's start with something simple that we can debug. I just added a section on Testing to the answer. Please give it a try and let me know what happens. – John1024 Sep 1 '16 at 17:52
  • It worked to add my code into ~/.local/share/mc/bashrc. Bad thing is that gitprompt doesn't work. However it was sourced from . ~/.bash-git-prompt/gitprompt.sh;. But prompt in shell is gray(not colorized) and it doesn't contain information about changes in repo. :( – D3ryk Sep 2 '16 at 6:40
  • mc does not support colorized prompts. – John1024 Sep 2 '16 at 6:41
  • I think it's not true, because if I ran . ~/.bash-git-prompt/gitprompt.sh; from shell after I press Ctrl-o. It gives me collorized prompt. – D3ryk Sep 2 '16 at 6:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.