Investigate Commands in WSL Bash Shells

This blog post contains guidance for gathering information about commands available in bash shells, specifically under Windows Subsystem for Linux (WSL). TLDR: Use type, which, and file to determine whether a command is an alias or a function or where it is and its type.

To my knowledge, you can invoke four types of commands from bash shells:

  • Executables, which can be binaries compiled for Linux, binaries compiled for Windows including cmd.exe (in the case of WSL, and therefore any Windows program or anything explorer.exe, the start command, or even rundll32.exe can invoke), various types of Linux text scripts that identify their processor with the string after the first two characters that must be #!, as well as Windows batch, command, VBS, and other scripting files.
  • Aliases, which are simple substitutions that simplify invoking commands. For example, I alias clip to clip.exe to avoid typing the .exe extension in WSL shells.
  • Functions, which are like aliases, but generally more appropriate for logic beyond command name replacement, especially for long sequences of commands, commands that involve redirection and invoking other processes in the background, and anything that processes command line arguments (alternatively, implement a shell script).
  • Shell commands such as cd to change the working directory.

Define aliases and functions at shell initialization.

When you type a command into the shell without specifying its location, I think that bash first looks for an alias, then a function, then through the directories specified by the $PATH environment variable for an executable.

You can get help about shell commands by typing help or hep command, such as help cd.

You can use man to get information about some installed executables, such as man egrep.

Some commands provide usage information if you supply the /h, /?, -h, -?, or other arguments. Beware that shutdown.exe -? provides help for the command while shutdown.exe /h hibernates the machine. Aliases and functions do not generally provide usage information.

You can use the type command to determine which type of command to invoke. If the command that you specify is an alias or a function, the type command will list its definition. If the command that you specify is executable, then the type command will show its location.

The which command will also show the location of an executable.

The file command will indicate the type of a file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: