• Home
  • Article
  • Windows Subsystem for Linux Guide Part 2 Command

Windows Subsystem for Linux Guide Part 2 Command

Execution of WSL side command by wsl.exe on Win32 side is as follows

ftype | wsl --egrep -i "^[^=]*\.(jpeg|jpg)"

This can be executed in cmd.exe Yes (Photo 01). It passes the output of ftype (internal command of cmd.exe) to the Linux command egrep (regular expression search) for processing.

Launch a Win32 command in WSL

The same as cmd.exe /c ftype | egrep -i '^[^=]*\.(jpeg|jpg)'

. The ability to run Win32 commands in WSL is called "Win32 Interop".

In this case, it is necessary to specify the executable file name of the command on the Win32 side, so be sure to add the extension .exe. Also, when executing an internal command of cmd.exe (such as the above ftype command), specify it as an argument of the "/c" option of cmd.exe. In both of the above examples, the left side of the pipe symbol "|" is the Win32 command and the right side is the WSL command. (Table 02) shows the writing pattern of Win32/WSL mixed commands using such a pipe.

Be careful when using the pipe symbol or vertical bar character “|” on the Win32 side as the pipe symbol or vertical bar character on the WSL side. cmd.exe and PowerShell recognize and process the pipe symbol. For this reason, if you want WSL to recognize it as a pipe, put an escape character ("^" for cmd.exe, "`" for PowerShell) before the pipe symbol so that cmd.exe and PowerShell use it as a pipe symbol. do not interpret.

Windows Subsystem for Linux Guide Part 2 Commands

If you use a file path as an argument to a command, it's up to each command to interpret the path, and you should follow its conventions. If you specify the path as an argument when executing the WSL side command from the Win32 side, use the Linux format. When starting a Win32 command on the WSL side and using a path as an argument, specify it in the normal Windows format. However, in bash, the Win32 side path delimiter "\" is an escape character, so specify it with single quotes.

Character code for standard output

When Win32 and WSL commands are mixed in a pipe, basically the character code (character encoding) is not automatically converted. Either convert accordingly.

If the Win32 side is cmd.exe, you need to pay attention to the character code (character encoding) of the Win32 command output in a Japanese environment. In principle, the output of Win32 commands follows the codepage specification, but depending on the command, the character code different from the codepage may be output. For example, sc.exe, which outputs a list of Windows services, outputs services with Japanese names in Shift JIS code regardless of the code page setting (Photo 02).

A code page specifies the character code (character encoding) used when displaying messages on the console. In the Japanese environment, the code page is basically "Shift JIS" (code page number 932), and in the US English environment, 437 is used. You can check and change the code page with the internal command chcp of cmd.exe.

When executing Win32 commands from the WSL side, the code page is specified according to the Windows "System Locale" setting (Control Panel ⇒ Region ⇒ Administrative tab ⇒ Language for non-Unicode programs). In the Japanese version of Windows, "Japanese (Japan)" is set here by default, and when executing Win32 commands from WSL, 932 (Japanese Shift JIS) is specified as the code page. However, if you select the checkbox for "Beta: Use Unicode UTF-8 with Worldwide Language Support", Win32 commands launched from WSL will have an environment with 65001 (UTF-8) specified as the code page. used.

When command output is displayed on the console, Win32 commands assume Shift-JIS (if the codepage is 932) and WSL commands assume UTF-8. For this reason, if the command output is directly displayed on the console, the characters will not be garbled, but conversely, if the output is done with a character code that is contrary to assumptions, the characters will be garbled (Photo 03).

When you look at the console output of the WSL command, it looks like the output is converted to Shift JIS because it looks correct, but be aware that text passing between commands does not do the conversion.

Package Manager

One thing you cannot avoid when using WSL is package management for your Linux distribution. A package is a collection of programs, libraries, configuration files, etc. for each piece of software. In the current Linux, programs can be searched and installed using a package collection site called a repository installed on the Internet. Here, for the time being, I will briefly explain the package manager commands, and I would like to leave a detailed explanation for another occasion.

There are many packages, users can search for them with search function and install what they need. At this time, the package manager automatically installs the necessary libraries according to the information described in the package.

(Table 03) is a list of apt commands for the dpkg-based package manager used in Ubuntu. The apt command has subcommands, which are combined with arguments to manage packages. For example, to search for packages by "keyword":

sudo apt updateapt search Keyword

. A search pattern using regular expressions can also be specified for the "keyword". Note that some apt commands require administrator privileges. In this case, it is necessary to prefix the apt command with the sudo command to execute the apt command with administrator privileges.

Use the “apt show packagename” command to see information about a discovered package, and the “apt install packagename” command to install a package. If you try to execute a command that is not installed, an error message may show the package name, etc. (Photo 04).

Mixing Win32 and WSL commands on the command line is the main point of WSL. Of course, you don't have to mix the commands forcibly, you can use each one individually, but you can use Win32 commands in bash just like cmd.exe, and cmd.exe can also use useful Linux commands such as grep It should be noted that the availability of is different from Linux running in a virtual environment. Lastly, the keywords used in this article are explained in (Table 04).