Operating Systems

Unix Shell (bash) / Windows Console (CMD.EXE)

[ Windows Command Prompt ] [ Back to Lecture Notes ]

Task bash CMD.EXE
Change directory
cd
cd
Display current working directory
pwd
cd
Create text file from console
  • ^D = Ctrl-D; sends End of File (EOF) in bash
  • ^Z = Ctrl-Z; sends EOF in Windows/DOS
$ cat >test.txt
This is a test
This is line 2
^D
$
C:\> copy con test.txt
This is a test
This is line 2
^Z
        1 file(s) copied.

C:\>
Echo contents of text file to console
$ cat test.txt
This is a test
This is line 2
$
C:\> type test.txt
This is a test
This is line 2

C:\>
Set an environment variable
$ MY_VAR=TEST
$
C:\> set MY_VAR=TEST

C:\>
Echo value of environment variable to console
$ echo $MY_VAR
TEST
$
C:\> echo %MY_VAR%
TEST

C:\>
Append directory to end of PATH variable
PATH=$PATH:~/csci142/lab3
PATH %PATH%;d:\csci142\lab3
List contents of directory:
  • detailed listing
  • "wide" listing (file only)
  • all files (hidden, system)
 
  • ls -l
  • ls
  • ls -a
 
  • dir
  • dir /w
  • dir /aa
List environment variables
set
set
Clear console screen
<Ctrl-L>
clear
cls
Command recall
  • For Win95/98 enter the command: doskey
    at start of each command prompt session to enable command recall
<Ctrl-P>, <Ctrl-N>
Up-arrow, Down-arrow
Up-arrow, Down-arrow
Echo command history to console
history
cat ~/.bash_history
doskey /h
Alias / keyboard macro
alias NAME=command
doskey NAME=command
Perl "one liner"
  • Windows only supports double quotes for quoting; all internal double quotes must be escaped.
  • WinNT/2K/XP has some additional (mostly undocumented) quoting and escaping features.
$ perl -e 'print "Hello World\n";'
Hello World
$
C:\> perl -e "print \"Hello World\n\";"
Hello World

C:\>
Executable Perl script

Windows Notes:
  • -x " (lower case x):
    Skip all text until shebang line.
  • -S " (upper case S):
    Look for script using PATH variable.
    Special meaning in Windows: appends .bat or .cmd if lookup for name fails and name does not have either suffix.
  • %* " only on WinNT/2K/XP; use %1 %2 . . . %9 on Win9x/DOS
hello.bat.txt
$ cat hello
#!/usr/bin/perl
# hello
# UNIX executable Perl script
# Set file attributes/permissions
#   as follows:  -rwxr--r--
#
# $ chmod 744 hello

print "Hello, World!\n";
# --------- end of hello ---------

$ chmod 744 hello
$ ./hello
Hello, World!
$
C:\> type hello.bat
@echo off
:: hello.bat
:: Windows executable Perl script
:: Note:
::   assumes perl.exe is in path
::   otherwise, use absolute path
perl -x -S "%0" %*
goto end
#!perl

print "Hello, World!\n";
__END__
:end
:: ------ end of hello.bat ------

C:\> hello
Hello, World!

C:\>
[ TOP ]

www.mhuffman.com/notes/dos/bash_cmd.htm
Mike Huffman: