Configure Eclipse to use bash login shell for Cygwin toolchain - eclipse

I have a custom Makefile project in Eclipse and although the build does get run in a Cygwin shell... it does not seem to be a login shell (bash --login) as it doesn't set my environment variables like running cygwin.bat does.
Where in Eclipse can I change the shell command so that it will be a login shell?

What you actually aim with bash --login are your settings from /etc/profile.
Under UNIX you normally have only one login shell and so these settings are inherited by all other shells. Under Windows any Bash window is an isoloated login shell, which leads to missing environment settings when running Bash from tools that run bash simply as command processor.
I had a similar problem with Emacs compile feature. The best solution under Windows is to set the environment variable BASH_ENV to a script. Bash will execute this script when started without -i or --login, so that /etc/profile is not run. Hence the script will setup Bash for non-interactive, non-login shells.
Example:
BASH_ENV=%USERPROFILE%\.bash_env
as user environment variable. The least thing to do in this script is to set PATH as in /etc/profile:
PATH="/usr/local/bin:/usr/bin:${PATH}"
Check the path-settings in /etc/profile as it is created by Cygwin's setup.exe. You may also copy settings from ~/.bashrc or source this script.
Hope this helps.

Related

How to use Python3 on the VScode terminal?

Is there a way to force VS Code to use only python3? It always defaults to python2.7 no matter what I try. I've tried selecting the correct interpreter as python3.7. When I open up terminal, it immediately uses python2.7, In the settings it is pointing at 3.7, but the built in terminal which is nice, always defaults to 2.7.
First, understand that the integrated terminal of VSCode, by default, uses the same environment as the Terminal app on Mac.
The shell used defaults to $SHELL on Linux and macOS, PowerShell on
Windows 10 and cmd.exe on earlier versions of Windows. These can be
overridden manually by setting terminal.integrated.shell.* in user
settings.
The default $SHELL on Mac is /bin/bash which uses python for Python2.7. So VS Code will just use the same python to mean Python2.7. When you open a bash shell, it will load your ~/.bash_profile to apply custom aliases and other configurations you added into it.
One solution to your problem is edit your ~/.bash_profile to alias python to python3. But I do not recommend this because this affects all your bash sessions, even those outside of VS Code. This can lead to nasty side effects when you run scripts that need python to be the system Python2.7.
You can instead configure VSCode to load its own aliases, for its own integrated terminal. First, create a file named vscode.bash_profile in your home directory:
$ cat ~/vscode.bash_profile
alias python=$(which python3)
On my env, python3 is Python3.7. You can set it to the what's applicable on your env (ex. maybe python3.7). Then, in VS Code, look for the Terminal shell args setting:
and then open your settings.json and add these lines:
"terminal.integrated.shellArgs.osx": [
"--init-file",
"~/vscode.bash_profile",
]
Finally, restart VS Code. The next time you open the VS Code terminal, python should now be using your Python 3 installation. This should not affect your bash session outside of VS Code.
Note that, if you have some custom settings from the default ~/.bash_profile, you may want to copy it over to your ~/vscode.bash_profile, so that you can still use it on VS Code (ex. changes to PATH, git-completion scripts..).

Run a virtual environment globally?

Is there any way to make workon global? For example; I open terminal and type workon myenvname --global, then I open another terminal window and type something like python myscript.py and it will run it under the myenvname environment?
Also I can then open Sublime Text IDE and create a python script, then press CTRL+b and the python script will run in the myenvname environment.
Is such a thing possible?
No. Virtual environments must be activated in every shell (i.e., every terminal) separately.
There is no magic in Python virtual environments. Their activation just sets a few environment variables; the most important is PATH so that the current shell finds python and pip. Then python being run from a virtual env detects it and sets sys.path accordingly.
To some extent you can do a trick without activation: run python from a virtual env:
/path/to/venv/bin/python myscript.py
Hope it helps someone
You may not be able to run it globally but if you want to run a particular python script without entering into the virtualenv. Here's a work around.
Considering you are using this on linux.
Say you have a virtualenv called myenvname
You would like to run a particular python script myscript.py inside this particular myenvname without even using terminal by just double click on an icon.
Create a shell script myshellscript.sh
#!/bin/bash
# open the virtual environment
source /home/usr_name/.virtualenvs/myenvname
# location to the python script you want to run
# python/python3 depending on the version you are using
python location/to/your/python/script/myscript.py
Give permission for myshellscript.sh to be an executable
chmod +x myshellscript.sh
Create a .desktop file inside /usr/share/applications/
sudo nano /usr/share/applications/myscript.desktop
copy paste the block of code and make the changes accordingly
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=shellscript file to be run along with its path
Path=directory where the file is located
Name=myscript
Comment=comment here
Icon=icon path here
then give permission to the myscript.desktop
chmod +x /usr/share/applications/myscript.desktop
You are done.
Just goto /usr/share/applications/ and double click on myscript icon, you got your myscript.py running

How to open cygwin & execute bash in Eclipse?

I am trying to build my custom-commands in Eclipse.
This is Eclipse plugin which I am using: https://marketplace.eclipse.org/content/startexplorer
It looks like this (link to image):
Custom commands in Eclipse
I need eclipse variables in custom commands:
${resource_loc} , ${selected_resource_loc} , ${workspace_loc} , etc...
It should be something like this:
D:\cygwin64\bin\mintty.exe /bin/bash -l -c "cd ${workspace_loc}"
But mintty will close this window immediately. I need to execute command based on eclipse variable and go to bash interactive mode, without closing window.
To create a StartExplorer custom command, that opens a Cygwin terminal and
starts an interactive Bash shell in the filesystem location of the selected
resource, follow these steps:
Make sure to install chere
Cygwin package;
Install StartExplorer
Eclipse plugin;
In Eclipse Preferences for plugin StartExplorer, create a new custom
command:
Command: D:\cygwin64\bin\mintty.exe -e /bin/xhere /bin/bash
"${selected_resource_loc}"
Enabled for Resources: yes
Name for Resources Menu: Cygwin Bash Here
Resource Type: Folders
Alternatively to steps 2 and 3, if you don't care about context-menu entry, no
need to install the StartExplorer plugin.
Eclipse Extenal Tools Configuration standard feature will do the trick.
In Run > Extenal Tools Configuration, create a new Program:
Name: Cygwin Bash Here
Location: D:\cygwin64\bin\mintty.exe
Arguments: -e /bin/xhere /bin/bash "${selected_resource_loc}"
Basically, the xhere script (part of chere package) performs the following steps:
indicate to the login shells not to cd $HOME (export CHERE_INVOKING=true, which is checked for in /etc/profile);
change to the directory passed as 2nd argument (cd "$2");
Execute the shell passed as 1st argument as a login shell (exec -l $1).
Note: if you replace /bin/bash with /etc/passwd, the current user's login shell read from /etc/passwd is used instead of bash.

How to change default eclipse shell interpreter?

I have installed the ShellEd plugin in eclipse. Whenever I run a bash script it runs it using dash even if I specify interpreter directive #! /bin/bash at the top of the script.
How do I change this behavior?
You need to install Cygwin or MSYS/MinGW.
Add the interpreter bash.exe path in eclipse as follows:
Window->Preferences->Shell Script -> Interpreters
THere should be an option to set the bash interpreter as follows:
Window->Preferences-><the_plugin_name>->Interpreter.
you need to set you bash interpreter path here (/bin/bash).
Eclipse will use the value you set here to execute your program.
In new versions of Eclipse current v2.23.0 and beyond the path to change terminal is:
Windows > Preferences > Shell > Local Terminal, under subtitle Show in there is a button labeled Add

How to get Powershell to run SH scripts

Can someone please tell me how I get Powershell to run .sh scripts? It keeps just trying to open the file i need in Notepad when I run this command:
.\update-version.sh 123
I've added .SH to my PATHEXT environment variable and also added the Microsoft.PowerShell_profile.ps1 directory to the Path environment variable.
PowerShell scripts have a .ps1 extension (along with other supported extensions such as psm1,psd1 etc) it will not run other extensions other than that.
I had also got the same issue when i am trying to run .sh files. The solution is you need to enable bash shell by typing bash.After enabling Bash on Windows, you can run .sh files by typing bash ./fileName.sh. But i have tried through git bash with typing . fileName.sh it worked well. These are the two solutions that I have found, and you can try whatever you wish.