Starting embedded MSYS bash in the project directory in clion - bash

How can I start the embedded terminal in Clion in the project directory if I am using a custom terminal (like MSYS or Cygwin bash.exe)?
Bash always seems to start in the home directory no matter what the startup directory is set to. Is there a way to force bash to start up in the directory that it is ran in? I tried adding
cd "`pwd`"
to the .bashrc, but that didn't work.
Here's a screenshot of the terminal settings page in Clion:

I solved that by using a windows batch file with the content
#echo off
set CHERE_INVOKING=1 && C:\dev\msys64\usr\bin\bash.exe --login
Then I put the batch file in CLion.
I derived that from the msys2 start script msys2_shell.cmd where the environment variable CHERE_INVOKING is used to specify that the calling directory is kept.

Related

Running shell script on Windows via Cygwin mintty.exe doesn't execute runcoms

Problem summary
I'm trying to launch a .sh script via Windows 10 Cygwin (i.e. mintty.exe > bash.exe) but neither .profile, .bash_profile, or .bashrc are loading, which I need to update PATH env variable with Cygwin's bin directory.
Background
I'm trying to launch a script finder.sh:
#!/bin/bash
find .
read
from C:\Users\Bo\Temp\. It has unix line endings and executable bit set.
I have Cygwin installed at C:\Users\Bo\AppData\Local\Programs\cygwin64\. I do not have this path in either System or User Windows' Environment Variables (and I don't want to!). My runcoms all live in this directory under /home/Bo. My .bash_profile (and ATM .bashrc) have an export PATH="/cygdrive/c/Users/Bo/AppData/Local/Programs/cygwin64/bin":${PATH} in them.
I want to launch the script from Windows Explorer. I tried using the bash.exe and mintty.exe in the cygwin64\bin\ folder via Open > Choose another app > More apps > Look for another app on this PC. In either case the mintty window displays:
FIND: Parameter format not correct
meaning the Windows' find command was used not Cygwin's. So I have my script echo $PATH and the Cygwin/bin directory is not in PATH. If I add the proper export PATH statement from above to my own script it works fine. So, now to debug the launcher and runcoms...
I've put echo ${0} statements in .profile, .bash_profile, and .bashrc, none of which trigger which I run the .sh script, they are never run. I've read SO and the mans. I've tried creating a Shortcut to both mintty.exe and bash.exe passing a variety of -l -i -e - commands to each using Properties > Shortcut > Target and they are never run. E.g. running simply [..]\mintty.exe -h always doesn't even leave the window open.
How do I get my script to run in Windows Explorer via Cygwin's mintty.exe/bash.exe, and to read from a runcom to update PATH (to find Cygwin Linux commands, vs. updating Windows Environment Variable)?
Two part fix:
A) set a Windows Environment Variable for BASH_ENV to a .bash_env under your Cygwin HOME, and export the PATH variable to include the Cygwin/bin directory from that file. I cannot find a decent reference for this in Cygwin documentation because it seems to be simply a bash thing, but this variable is what bash looks for when running non login/interactively. Best reference: Cygwin shell doesn't execute .bashrc.
And B) run the .sh with bash.exe from Cygwin/bin using Open With....
ALSO, annoying Windows bug: when you select a program to Open With... your .sh script, it will always run 1x with a CWD from your C:\Windows\System32 directory(?!) and all other times will run fine with the CWD as the directory from your .sh script.

How to create a .bat file which execute python file

I'm new to bat file and started to implementing it. I have a list of linux application commands which starts my application. I have a windows system to deploy, used to git bash to execute those commands, but in every system restart have to start the application manually so I started implementing bat file which mapped in system start up
#echo off
title ML_autostart_API
start "" "C:\Program Files\Git\git-bash.exe"
using the above script I've opened the git bash. In further need to perform below commands
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
have to execute the above using git bash since it is open source commands and doesn't execute in windows. git bash is opening and further commands is not working.
You will need to make 2 files, one for the windows command line (.bat) another for the bash script (.sh). The reason being, after you start the bash console, it will work on different window and it has no idea what your .bat contains. We shall call our scripts as boot.bat and start.sh respectively.
boot.bat
#echo off
title ML_autostart_API
start "C:\Program Files\Git\git-bash.exe" start.sh
Notice the start.sh is added at the end of the start command as parameter.
start.sh
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
Note
Both scripts are in the same directory.
This answer assumes python is actually recognized in git-bash paths.
Should this is not the case, you can just use the full path to the executable to call it.
A better alternative would be to just execute the bash script directly on start up (using that start "C:\Program Files\Git\git-bash.exe" start.sh), no mixing stuff.

Get c++ commands in cmd

I am a Windows 8 user.
How would I get command such as make and g++ in my cmd window? I got javac stuff from downloading jdk. How to do for c++? Tanks
To achieve similar results, you would need to compile code into an executable of some sort (ie, C:\Dev\myfile.exe) and add the directory of where the file resides into your system PATH environment variable (ie, C:\Dev) or change the directory of your command prompt to that directory via cd C:\Dev.
Then you can invoke your executable directly in the command prompt,
C:\>myfile # assuming the directory is setup in PATH environment variable
or
C:\Dev\> myfile # navigate to folder and invoke executable directly
How to Run a Program

How do I launch a Git Bash window with particular working directory using a script?

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?
My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.
I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.
Another option is to create a shortcut with the following properties:
Target should be:
"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
Start in is the folder you wish your Git Bash prompt to launch into.
Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:
"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"
If used inside registry key, folder parameter can be provided with %1:
"C:\Program Files\Git\git-bash.exe" --cd="%1"
Git Bash uses cmd.exe for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exe wrapper. In Windows you launch a new terminal using the start command.
Thus a shell script which launches a new Git Bash terminal with a specific working directory is:
(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &
An equivalent Windows batch script is:
C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).
Let yet add up to the answer from #Drew Noakes:
Target:
"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo
The cd param should be one of the options how to specify the working directory.
Also notice, that I have not any --login param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).
Start in:
C:\GitRepo
The same possible way, as #Drew Noakes mentioned/shown here sooner, I use it too.
Shortcut key:
Ctrl + Alt + B
Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.
If you do not want it visible, yet still activatable, place this .lnk file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355
"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.
Windows 10
This is basically #lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.
cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:
Create a new key called Bash in the shell key at HKEY_CLASSES_ROOT\Directory\Background\shell
Add a string value to Icon (not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
Edit the default value of Bash to the text you want to use in the context menu
Add a sub-key to Bash called command
Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC is not a real directory.
This is the command which can be executed directly in Run dialog box (shortcut is win+R) and also works well saved as a .bat script:
cmd /c (start /d "/path/to/dir" bash --login) && exit
I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshell and your favorite terminal program is called `myterm' you can script the following:
(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...
Note that the parameter -e for execution may be named differently with your favorite terminal program.
Using Windows Explorer, navigate to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.
Along the same lines, if you have the git directory in your path, you can type "git-bash" in the address bar and a Git Shell will open in that directory.
If using Windows OS :
Right click on git terminal > Properties
Properties>Under shortcut tab>Start in:
add your folder target path like below image

executing a custom init script for bash --login -i for example to change to a custom directory from a shortcut

Right now I'm using MSysGit on Windows 7, which is started from a .bat file, which itself calls bash.exe --login -i to start a shell. At which point it executes the .bashrc file (among others) in the user's home directory. I use this script to setup the environment and cd to a starting directory.
This all works fine. What I would like is to change the .bat file in some way so that bash will execute a custom script at startup so that in that script i could perform a different initialization and cd to a different starting directory. Then I could have two separate .bat files calling each script, then I could make a shortcut to both on the desktop and start whichever I want.
The thing that I'm not sure how to do, is get bash to run a custom init script on startup. Currently the command MSysGit uses is bash.exe --login -i. Is there any way I can modify this to get it to use a custom file? I tried bash.exe --login --rcfile 01.txt -i but that didn't work. Likewise nothing else I tried worked either.
Try it without --login:
bash.exe --rcfile 01.txt -i

Resources