How to start WSL terminal in a current powershell directory? - powershell

I can open a powershell and type
> Ubuntu
to open a WSL ubuntu shell on windows 10. This will always take me to the WSL home directory. How would I instead open the terminal in the same location that the powershell is currently in?
FYI. I need this for creating a right click "open terminal here" type registery key for windows explorer.

If we look at what running ubuntu.exe actually does:
PS C:\> ubuntu.exe /?
Launches or configures a Linux distribution.
Usage:
<no args>
Launches the user's default shell in the user's home directory.
install [--root]
Install the distribution and do not launch the shell when complete.
--root
Do not create a user account and leave the default user set to root.
run <command line>
Run the provided command line in the current working directory. If no
command line is provided, the default shell is launched.
config [setting [value]]
Configure settings for this distribution.
Settings:
--default-user <username>
Sets the default user to <username>. This must be an existing user.
help
Print usage information.
We can see that it by default launches the WSL shell inside the home directory. If we want run inside it inside the current directory open in PowerShell, we need to specify the run option. So the full command will be ubuntu.exe run.
Another option is to just run wsl.exe or bash.exe. These commands will by default open WSL in the current working directory.
Note: We don't need to specify the .exe after the commands. Running ubuntu, wsl and bash all work as well. PowerShell knows how to run executables without specifying the extension.

method 1:
set Ubuntu to wsl default distribution by wsl --set-default Ubuntu
just type wsl and you are in the current Powershell directory
method 2:
open other folder
wsl.exe --cd $pwd or wsl.exe --cd "path"

Related

How to open a tab/pane in the same directory in Windows Terminal with Fish shell

Microsoft has a guide how to do that for other shells such as bash and zsh but not for fish shell.
https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory
open your fish config file located at ~/.config/fish/config.fish and add to it the following function:
function storePathForWindowsTerminal --on-variable PWD
if test -n "$WT_SESSION"
printf "\e]9;9;%s\e\\" (wslpath -w "$PWD")
end
end
Explanation:
This function is a hook that is called whenever current path is changed.
It will confirm current session is opened by Windows Terminal (By verifying environment variable $WT_SESSION exists), and will send Operating System Command (OSC 9;9;), with the Windows equivalent path (wslpath -w) of current path.
This sequence will let Windows Terminal know that the path was changed and when a new tab / pane will be created, Windows Terminal will use the changed path as current directory.

Yarn Install - Not recognized as command in Windows 11 Powershell

Yarn's not recognized as a cmd command.
I tried installing it globally with 'npm install -g yarn' and adding a path variable to "C:\Users[user]\AppData\Roaming\npm\yarn".
Any other suggestions?
First thing first, you should check you have 16+ node.js installed and, you can check it with
node -v
If it's valid,
run the npm config get prefix it'll give you a path CTRL+C this path
Open the start search and type in env and then click "Edit the system environment variables"
Modify the Environment variables
Add the copied path to you PATH as new
After this you should be good, but if the PS is cannot load the Yarn run this command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Here a few things you can try:
You may need to close and reopen the Powershell windows because newly added environment variables or updates to them are not automatically reflected to already open shell windows.
Check if your path is correct: it normally is C:\Users\endo\AppData\Roaming\npm on my PC, not C:\Users\endo\AppData\Roaming\npm\yarn as you mentioned.
Try running yarn.cmd or yarn.ps1.
Try CMD instead of Powershell, see if it works that way.

WSL Interop PATH customization

I have an Ubuntu-18.04 WSL instance installed and have a large set of scripts installed in it at /home/username/bin/scripts.
I have also updated the $HOME/.bashrc, $HOME/.bash_profile, /etc/bash.bashrc, /etc/profile and /etc/environment files to update $PATH to include /home/username/bin/scripts in the path.
*I'm well aware i only usually need it in my $HOME/.bash_profile, lots of places from experimenting
Say I have a script called sampleScript in /home/username/bin/scripts, when i try to run the command:
wsl sampleScript
It gives the error: /bin/bash: sampleScript: command not found
If I open up wsl in interactive mode I can run it just fine, but I would like to be able to expose the script to not be run in interactive mode.
I've tried making an environment variable SPATH='/home/username/bin/scripts' and setting WSLENV=SPATH/p, but it also does not show up if i do wsl echo $PATH.
*I'm doing this all in command line instead of powershell b/c powershell doesn't appear to be able to do wsl echo $PATH at all, it just returns empty
I have also tried: wsl -u username sampleScript; with no luck
Is there a way to customize the PATH for wsl interop mode or am i doing something incorrectly with WSLENV?

VSCode terminal windows - git-bash aliases getting ignored

I've created aliases in c:\Users\user\.bash_profile and in C:\Program Files\Git\etc\profile.d\aliases.sh but both configs getting ignored by VSCode integrated terminal, which is configured to use git bash:
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
if I open GitBash itself - aliases works fine
how do I force integrated terminal to respect the configs?
You can try adding to the settings:
// The command line arguments to use when on the Windows terminal.
"terminal.integrated.shellArgs.windows": [
"--login", "-i"
],
-i - force the shell to run interactively.
--login - make this shell act as if it had been directly invoked by login. When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files.
Read more.
As an alternative you can use the .bashrc file instead of .bash_profile.
The solution using the "--login" and "-i" shell arguments did not work for me. What did work was using the bash "-rcfile" shell argument, like this, in my settings file:
"terminal.integrated.shellArgs.windows": [
"-rcfile",
"c:\Users\\.bash_profile", ],
... where <userid> is my Windows userid and the alias commands are in a file called ".bash_profile" that is located in c:\Users\<userid>
I simply switched to my root user directory c:\Users\user then ran source .bashrc. This did the trick on my machine, hope it helps.
Create .bashrc in c:\Users\username
Add an alias, e.g alias gotossh="cd /c/users/username/.ssh"
In your terminal, run source .bashrc while in c:\Users\username
Confirm that the alias works by running alias
You might not have a .bashrc file in the users folder which is important to note.
Also remember to write the paths correct and don't leave any space between the equals in e.g alias="somecommand"
As of 2020 there is no need to add the shellArgs.

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"