Linux Bash shell on Windows 10

The Windows 10 Anniversary Update allows you to install the full Ubuntu based Bash shell on Windows 10. This includes basic the Linux system utilities you would expect and you can 'apt-get' to download many more GNU software packages. You don't even need to recompile Linux binaries - I have added my Vancouver Utilities compiled under Red Hat Linux and executed without recompiling.

This is a tutorial that will help you master using Linux bash on Windows 10. I suggest you print it out & follow the step by step instructions. Note that I have underlined (with '='s) the commands you need to enter.

Install Windows 10 Anniversary Update

Download & install the Anniversary Update as follows:


 #1. Start --> Settings --> Update & Security --> Check for update
     =============================================================
 #2. select Feature update to Windows 10 version 1607
     =============================================
     note - if, when you click start, "Settings" is not visible
          - move cursor right to show the scrollbar & scroll down to "Settings"

Install Linux Bash on Windows 10


 #1. Start --> Settings --> Update & Security
     ========================================
 #2. click "For Developers" (on left 2nd from bottom)
     ================================================
 #3. select "Developer mode" bullet
     ==============================

Next, go to "Programs & Features" to enable "Windows Subsystem for Linux"


 #4. Start --> Control panel --> Programs & Features
     ===============================================
 #5. click "turn Windows Features on/off" (on left of program list)
     ==============================================================
     - displays list of checkboxes for various Windows features

 #6. scroll down, checkmark "Windows Subsystem for Linux" & click OK
     ================================================================
 #7. select prompt to reboot computer to install new feature

Goto:   Begin this document End this document UVSI Home-Page

Activate Bash using a Command prompt window

After computer restart, you could use Start --> Bash to setup the bash directories & files interactively, but this did not work for me. I recommend using a command window to automate the install with the "lxrun" command and option to auto agree to all prompts.


 #1. Login on Windows (as an Administrative user) & open a Command Window

I used 'Owen' as my Windows10 admin user (vs 'owen' Linux bash user). Maybe I should have used 'OwenWin10' or 'Win10adm' to avoid confusion. Note that each Windows10 userid may have several Linux userids. Also note that each Windows10 userid desiring to use Linux must install the Linux file system separately under that Windows10 userid.


 #2. Start --> Command prompt
     ========================
 #3. lxrun /install /y
     =================
     note - this takes a long time (30 minutes ?)

Bash File System on Windows 10

Each Windows user will have their own separate Bash system files under their windows user directory. These will be hidden files and you can change this using Windows Explorer View options. But you will always see them using the command prompt window. The Bash system files are installed under the 'AppData' directory in your windows user directory. Here are the most significant directories.

 C:\Users\USERNAME\AppData\Local\lxss\
 C:\Users\Owen\AppData\Local\lxss\     <-- Windows user 'Owen'
 :-----home
 :     :-----owen           <-- Linux user 'owen' (or Your name)
 :     :-----appsadm        <-- used to store env/common_profile, etc
 :     :-----uvadm          <-- for Vancouver Utilities (optional)
 :     :-----mvstest        <-- to test mainframe conversions (optional)
 :     :-----xxxx           <-- other userids desired
 :-----mnt
 :     :-----c           <-- Windows file system
 :     :-----d
 :-----root              <-- Linux root homedir
 :     :-----.profile
 :-----rootfs            <-- linux system files
 :     :-----boot
 :     :-----dev
 :     :-----etc
 :     :-----...

Appdata is setup as a 'hidden file', you can make it visible with the 'view' options in Windows File Explorer. For the Command Window as follows:


 attrib -s -h -r C:\Users\Owen\AppData   <-- ensure AppData visible
 =====================================

Goto:   Begin this document End this document UVSI Home-Page

starting bash & login

When you Login to Windows & enter 'bash' you will be logged into Linux as 'root'. You may then su (Switch User) to any desired user you have setup (with 'adduser'). Here are the login prompts that would appear - assuming you have setup the profiles recommended for the Vancouver Utilities, which display the userid and the current directory (using $LOGNAME & $PS1).


 #1. Login on Windows & open a Command Window

 #2. C:\Users\Owen> bash           <-- start bash (logs in as root)
                    ====
 #3. root:/mnt/c/Users/Owen> cd    <-- goes to the Windows User homedir
                             ===     - 'cd' will go to the root homedir
 #4. root:/root> su - owen         <-- Switch User to other Linux users
                 =========
 #5. owen:/home/owen> ...          <-- owen Linux user (vs Owen windows user)

Notes re login & directories

  1. Entering bash logs you in as root, but leaves you in your windows homedir - shown as /mnt/c/Users/Owen - you may address any Windows file under /mnt/c/...

  2. Entering 'cd' will take you to 'root's homedir shown as '/root'.

  3. Switch User will take you to that user's homedir '/home/uvadm' in this example

  4. Bash shows my files as residing under /home/owen/... This is the same and much shorter than: /mnt/c/Users/Owen/Appdata/Local/lxss/home/owen/...

  5. The prompts above show the 'Userid:CurrentDirectory' (see #3. #4. #5.), but you will not see them until you setup the PS1 prompt in the common_profile (see suggested common_profile listed later in this writeup).

    setup User Friendly Command Window

Before we go any further, I suggest you modify the properties of the command Window to make it much easier to work with. The default command window is an small window with ugly black background. I recommend dragging a Command Window prompt shortcut to the desktop, right-click, & modify the properties as follows:

 Layout window size Width ---> 120
 Layout window size height -->  55
 Color Background -----------> white
 Color Foreground -----------> black

Goto:   Begin this document End this document UVSI Home-Page

Setup Linux users

Here are the instructions to setup the 3 users (owen,appsadm,uvadm) shown in the directory diagram above. 'appsadm' will be used to store the common_profile (called by all linux user .profiles). 'uvadm' will be used to store the Vancouver Utilities if you have them; if not we will download the free 'uvhd' into uvadm/src/uvhd.c & compile into uvadm/bin/uvhd


 #0a. login to Windows & open a Command prompt
 #0b. C:\Users\Owen> bash      <-- start the Linux bash shell
 #0c. root:/mnt/c/Owen> cd     <-- change to root homedir (optional)
 #0d. root:/root> ...          <-- ready to perform root admin tasks

 #1. addgroup apps          <-- setup common group for linux users
     =============

 #2a. adduser --ingroup apps appsadm  <-- user to store profiles, etc
      ==============================
 #2b. adduser --ingroup apps uvadm    <-- for Vancouver Utilities (optional)
      ============================
 #2c. adduser --ingroup apps owen     <-- change to Your userid
      ===========================
 #2d. adduser --ingroup apps xxxx     <-- setup other users desired
      ===========================

 #3a. passwd appsadm    <-- Not required, adduser prompts for passwords
      ==============

setup stub & common profiles

I strongly recommend the 'stub' & 'common' profile concepts as explained at http://uvsoftware.ca/install.htm#A5. I suggest you create & store them in /home/appsadm/env/stub_profile & /home/appsadm/common_profile.

When you set up a new user, you will copy the stub_profile from /home/appsadm/env/stub_profile & rename as .profile (or .bash_profile) in the homedir of the new user. The stub_profile calls the common profile from /home/appsadm/env/common_profile.

The common_profile will setup the environmental variables, PATH's, aliases, PS1 prompts,& applications (such as COBOL, SQL Server, etc) that may be used by all users. The common_profile greatly simplifies administration, since the administrator can update the applications in one place for all users.

Below, we will setup minimal stub & common profiles. For examples of production profiles you might see http://uvsoftware.ca/jclcnv1demo.htm#1C1 & 1C2.

Goto:   Begin this document End this document UVSI Home-Page

setup minimal stub & common profiles

Assuming you do not have Vancouver Utilities, we will setup minimal stub & common profiles, with just enough code to compile & execute the 1 free download 'uvhd'.


 #0a. login to Windows & open a Command prompt
 #0b. C:\Users\Owen> bash      <-- start the Linux bash shell
 #0c. root:/mnt/c/Owen> cd     <-- change to root homedir (optional)
 #0d. root:/root> ...          <-- ready to perform root admin tasks

 #1. root:/root> su - appsadm          <-- Switch User to appsadm
                 ============
 #2. appsadm:/home/appsadm> mkdir env  <-- make dir for profiles
                            =========
 #3. vi env/stub_profile    <-- insert following code
     ===================
     # stub_profile - to be copied to user homedirs
     #              - & renamed as .profile or .bash_profile
     . /home/appsadm/env/common_profile
     #---------------------------------
     :wq

 #4. vi env/common_profile   <-- insert following code
     =====================
     # common_profile - to be called by stub profiles in user homedirs
     #                - to perform setups common to all users
     # - setup environment-variables,PATHs,aliases,prompts,applications
     export APPSADM=/home/appsadm
     export UV=/home/uvadm
     export PATH=$PATH:$HOME/bin:$APSADM/bin:$UV/bin
     export PATH=$PATH:$HOME/scripts:$APSADM/scripts:$UV/sf/util
     export PS1='$LOGNAME:$PWD> '  # setup command prompt
     alias l='ls -l'               # setup desired aliases
     # could setup applications such as COBOL, SQL Server, etc
     #---------------------------------
     :wq

 #5. Copy stub profiles to homedirs, renaming as .profile or .bash_profile

 #5a. cp env/stub_profile /home/appsadm/.profile <-- for appsadm
      ==========================================
 #5b. cp env/stub_profile /home/uvadm/.profile   <-- for uvadm
      ========================================
 #5c. cp env/stub_profile /home/owen/.profile    <-- for owen
      =======================================

Goto:   Begin this document End this document UVSI Home-Page

Access Windows files from Linux

Use '/mnt/c/...' to address Windows files from Linux bash, for example:


 #0a. login to Windows User 'Owen' & open a Command prompt
 #0b. C:\Users\Owen> bash          <-- start the Linux bash shell
 #0c. root:/mnt/c/Owen> su - owen  <-- login to Linux user 'owen'

 #1. owen:/home/owen> ls -l /mnt/c  <-- display files&dirs on the C: drive
                      ============
 #2. owen:/home/owen> ls -l /mnt/c/Users  <-- display files for Windows Users
                      ==================
 #3. owen:/home/owen> ls -l /mnt/c/Users/Owen  <-- files for Windows User 'Owen'
                      =======================
 #4. owen:/home/owen> ls -l /mnt/c/Users/Owen/AppData/Local/lxss/home/owen
                      ====================================================
                                           - display files for Linux user 'owen'
 #5. owen:/home/owen> ls -l /home/owen  <-- same, addressing via Linux vs windows
                      ================
 #6. owen:/home/owen> ls -l             <-- same, since we are in 'owen's homedir
                      =====
 #6. owen:/home/owen> l                 <-- same since profile alias l='ls -l'
                      =====

Goto:   Begin this document End this document UVSI Home-Page

writing scripts for Linux bash


 #0a. login to Windows User 'Owen' & open a Command prompt
 #0b. C:\Users\Owen> bash             <-- start the Linux bash shell
 #0c. root:/mnt/c/Owen> su - owen     <-- login to Linux user 'owen'
 #0d. owen:/home/owen> mkdir scripts  <-- mkdir for scripts if not already

 #1. vi scripts/test1   <-- create script test1 as follows:
     ================
     #!/bin/bash
     # test1 - test scripts under Windows 10 Linux bash
     #       - using typical Linux script commands
     export COMMENT="test1 - test scripts under Windows 10 Linux bash"
     echo "COMMENT=$COMMENT"
     echo "- will list files in current directory & exit"
     ls -l
     exit 0
     :wq

 #2. chmod 775 scripts/test1   <-- make script executable
     =======================
 #3. scripts/test1     <-- execute script
     =============
 #3a. test1            <-- works because common_profile includes
      =====                PATH=...:$HOME/scripts:...

Notes & Warnings

Carriage Returns x'0D' cause problems in Linux scripts, Lines must be terminated only with x'0A' - not with x'0D0A' as created by Windows utilities such as Notepad. Linux editors will not create CR's, but you may have a problem importing scripts created under Windows.

Goto:   Begin this document End this document UVSI Home-Page

Executing bash scripts under Windows (vs Linux)

You may execute Linux commands & scripts from Windows using 'bash -c' for example:


 #0a. login to Windows User 'Owen' & open a Command prompt
 #0b. C:\Users\Owen> ...     <-- at windows prompt, waiting for commands

 #1. C:\Users\Owen> bash -c "uname -a"  <-- show system information
                    ==================    - kernel name, version, etc

 #2. C:\Users\Owen> bash -c "C:\Users\Owen\AppData\Local\lxss\home\owen\scripts\test1"
                    ==================================================================
                    - execute the bash script we previously created above

 #3. It would be better to copy any scripts you intended to use under Windows
     from the Linux system where created (to avoid any CR's) to a Windows directory
     If we were in our linux directory, this would be

 #3a. owen:/home/owen> cp -r scripts/test1 /mnt/c/Users/scripts
                       ========================================

 #4. Then calling our Linux script from Windows is much simpler:

 #4a. C:\Users\Owen> bash -c "C:\Users\Owen\scripts\test1"
                     =====================================

Goto:   Begin this document End this document UVSI Home-Page

setup shortcut to execute Linux scripts

You are probably comfortable executing Linux scripts in the command window, but you may need to setup shortcuts on the windows desktop, for other users who prefer to work in the windows environment. Here is an example of setting up a shortcut to run the test1 script we created above.


 #0a. login in to Windows User 'Owen' at desktop window

 #1. Right click on an open space --> new --> shortcut

 #2. Click Properties, enter Location of Item & Shortcut Name
     Location of Item: C:\Users\Owen\scripts\test1  <-- enter
                       ===========================
        Shortcut Name: test1                  <-- defaults OK
                       =====
 #2a. Click Finish, but then update shortcut again as follows:

 #3. Right click on test1 shortcut
 #3a. Click Properties & insert 'bash -c' on target
           Target: bash -c "C:\Users\Owen\scripts\test1"
                   =====================================
         Start In: C:\Users\Owen\scripts  <-- default
                   =====================
         Start In: C:\Users\Owen   <-- remove '\scripts'
                   =============     - to address files in Owen homedir

 #4. Right click on test1 shortcut
 #4a. Click Properties to see final result:
           Target: C:\Windows\System32\bash.exe -c "C:\Users\Owen\scripts\test1"
                   =============================================================
         Start In: C:\Users\Owen
                   =============
Note
  • Windows automatically updates the Target
  • inserts 'C:\Windows\System32\' prior to your 'bash -c'
  • and changes 'bash' to 'bash.exe'

Executing shortcuts to Linux scripts

To execute Linux script shortcuts on the desktop, you would double click as usual, but you will not see any results on the screen, because the command window does not persist on the screen - even if you have a pause (read reply) at the end of your script. You will see the screen flash much too quickly to read anything. Let me know if you find a way to keep the command window open until you acknowledge.

Shortcut scripts are probably leaving their results in files that you may then use in Windows applications. These scripts could also create a log file in the windows file system (via /mnt/c/...) that you could easily check with wordpad.

Goto:   Begin this document End this document UVSI Home-Page

download Linux applications

For example, we will download the C compiler from the Ubuntu website via the 'apt-get' utility.


 #1. Turn off the Windows Firewall (temporarily, 1 hour)
     to prevent "cannot initiate connection"

 #2a. login to Windows & open a Command prompt
 #2b. C:\Users\Owen> bash      <-- start the Linux bash shell
 #2c. root:/mnt/c/Owen> cd     <-- change to root homedir (optional)
 #2d. root:/root> ...          <-- ready to perform root admin tasks

 #3. apt-get --fix-missing install gcc  <-- download & install C compiler
     =================================

 #4. apt-get update   <-- download info about available packaages
     ==============

 #5. apt-get upgrade  <-- download & install latest versions of your packages
     ===============

 #6. apt-get install xxxx  <-- install other packages desired ?
     ====================

 #6. apt-cache pkgnames    <-- list all packages available
     ==================

Note: There are over 40,000 packages & names are unsorted I suggest you redirect into a file, sort,& view with 'vi' as follows:


 #6a. apt-cache pkgnames >pkgnames      <-- redirect pkgnames into a file
     ============================
 #6b. sort pkgnames -o pkgnames_sorted  <-- sort pkgnames
      ================================
 #6c. vi pkgnames_sorted                <-- view pkgnames_sorted
      ==================

Goto:   Begin this document End this document UVSI Home-Page

download 'uvhd' & compile


 #1a. Point Internet browser to http://uvsoftware.ca/libuvhd.c
 #1b. click 'uvhd.c program source' & save to Downloads\uvhd.c
 #1c. Use Windows Explorer to copy from Downloads to C:\Users\Owen\uvhd.c

 #2a. Open a Command prompt
 #2b. C:\Users\Owen> bash           <-- start the Linux bash shell
 #2c. root:/mnt/c/Owen> su - uvadm  <-- login Vancouver Utilities Admin
                        ==========
 #3. uvadm:/home/uvadm> mkdir src bin  <-- make dirs required to compile uvhd.c
                        =============
 #4. cp /mnt/c/Users/Owen/uvhd.c src   <-- copy uvhd.c source to VU source dir
     ===============================

 #5. gcc src/uvhd.c -o bin/uvhd        <-- compile uvhd.c
     ==========================

 #6. uvhd bin/uvhd     <-- test uvhd by investigating itself
     =============       - displays 1st record & prompts for command
 version=20160921 records=730 rsize=256 fsize=187104
                   10        20        30        40        50        60
 r#     1 0123456789012345678901234567890123456789012345678901234567890123
        0 .ELF..............>.....`.@.....@.......(...........@.8...@.#. .
          7444000000000000003000006140000040000000280000000000403000402020
          F5C621100000000020E010000000000000000000882000000000008090003000

      --> s 'version'   <-- might search for version, etc
          ===========
                   10        20        30        40        50        60
 r#   277 0123456789012345678901234567890123456789012345678901234567890123
    70656          ** H01. HELP menu, uvhd version 20160921 **....H01. Hel
          2222222222224332244452666722776627677666233333333222000043322466
          000000000AA0801E085C00D5E5C05684065239FE0201609210AA0000801E085C
 found--> s 'version' <--at byte# 33 of record# 277
Notes
  • I am showing only the 1st 64 bytes of the default 256 bytes/record
  • we don't know a record-size, so uvhd displays chunks of 256 bytes
  • we find 'version' in 0 relative chunk #276 * 256 = 70656 bytes offset

Goto:   Begin this document End this document UVSI Home-Page

install Vancouver Utilities

If you have Vancouver Utilities, you could install them as documented at http://uvsoftware.ca/install.htm#B1 - B6. Those instructions also setup the user stub_profile in user homedirs & the common_profile at /home/appsadm/env/common_profile.

install the Korn shell

You can install other shells (ksh, zsh, etc) to run under the bash shell. The Korn shell is required to run many of the shell scripts provided with the Vancouver Utilities ('#!/bin/ksh' coded on line 1). The Korn shell provides FPATH (export FPATH=pathtofunctions), that allows us to store common functions in directories separate from the scripts calling them.

The Korn shell is a free download (google for "download korn shell") or if you have Vancouver Utilities, copy/rename from /home/uvadm/lib/ksh93 to /bin/ksh.

Goto:   Begin this document End this document UVSI Home-Page

Links to more information

 http://uvsoftware.ca                 - UV Software Home Page
 http://uvsoftware.ca/news/           - all newsletters past & present
 http://uvsoftware.ca/scripts1.htm    - most useful script listings
 http://uvsoftware.ca/jclcnv1demo.htm - JCL Conversion to scripts
 http://uvsoftware.ca/uvsort.htm      - uvsort documentation
 http://uvsoftware.ca/uvprices.htm    - prices & partner agreements
 http://uvsoftware.ca/uvintro.htm#G1  - customer list & reference letters
 http://uvsoftware.ca/libuvhd.htm     - download uvhd FREE utility

I invite your feedback. Let me know if these news letters are useful to you, and any suggestions you may have for improvements.

Thank You, Owen Townsend, UV Software Inc.

Goto:   Begin this document End this document UVSI Home-Page