Operating System Automation: POSIX Shell, Cmd.exe, PowerShell

a side-by-side reference sheet

grammar and invocation | variables and expression | arithmetic and logic | strings | regexes | dates and time
arrays | functions | execution control | exceptions | files | directories | processes and environment
libraries and namespaces | reflection

posix shell cmd.exe powershell
version used
 
dash; POSIX 2008 6.3 2.0
show version
 
displayed at startup $host.version
grammar and invocation
posix shell cmd.exe powershell
interpreter
 
$ dash foo.sh when foo.bat is in the search path:
> foo
PS> .\foo.ps1

DOS> powershell -file foo.ps1
repl
 
$ dash > cmd > powershell
execute command and exit $ dash -c 'echo hi' > cmd /c "echo hi" > powershell -command 'write-host "hi"'
statement separator pipelines separated by
; & && ||

lists of pipelines separated by newlines unless newline is preceded by a backslash or inside these characters:
"" '' `` ()
pipelines separated by & && ||

lists of pipelines separated by newline unless preceded by caret ^ which is not inside double quote "
; or newline; a newline can be escaped with a backtick: ` newlines are permitted in double quotes and after a pipe: |
word separators | & ; ( ) < > space tab
escape special character # write "foo" to foo.txt:
echo foo > foo.txt

# write "foo > foo.txt" to stdout:
echo foo \> foo.txt
rem write "foo" to foo.txt
echo foo > foo.txt

rem write "foo > foo.txt" to stdout:
echo foo ^> foo.txt
block delimiters {}
()
do done
( ) { }
are expressions statements no no

relational expressions can only be used in the conditional of an
if statement

arithmetic expressions can only be assigned to a variable using
set /a
yes
end-of-line comment
 
# comment rem comment # comment
comment out multiple lines <<EOF
comment
another comment
EOF
<# comment
another comment #>
variables and expressions
posix shell cmd.exe powershell
assignment a=1
whitespace next to = not permitted
set a=1 $a = 1
parallel assignment
 
none none $a, $b = 1, 2
swap tmp=$a
a=$b
b=$tmp
set tmp=%a%
set a=%b%
set b=%tmp%
$a, $b = $b, $a
compound assignment operators: arithmetic, string, bit none += -= *= /= %=
none
«= »= &= |= ^=
+= -= *= /= %=
+= *=
none
increment and decrement none none $x = 1
$x++
$x--
variable declaration
 
assignment, export, readonly assignment
variable evaluation
are identifiers case sensitive? yes no
null
 
'' $null
null test
 
[ -z $v ] $v -eq $null
undefined variable access '' $null
undefined test f [ ${foo+1} ]; then
  echo "defined"
else
  echo "undefined"
fi
test-path variable:\foo
arithmetic and logic
posix shell cmd.exe powershell
true and false
 
status codes:
true false

inside [ ]:
1 ''
no booleans; conditional of if statement must contain a relational expression $true $false
falsehoods status codes:
nonzero integers


inside [ ]:
''
relational expressions which evaluate to false 0 0.0 "" ''
logical operators
 
status codes:
&& || !

inside [ ]:
-a -o !
short-circuit operators:
&& ||

inside conditional of if:
not
-and -or -not
conditional expression
 
$(( x>0 ? x : -x )) none none
relational expression
 
[ $a -gt 3 ] %a% gtr 3 $a -gt 3
relational operators
 
integers:
-eq -ne -gt -lt -ge -le

strings:
= != > < none none
equ neq gtr lss geq leq -eq -ne -gt -lt -ge -le
arithmetic expression
 
$(( 1 + 3 )) arithmetic expression must be stored in a variable:
set /a "foo=1+3"
1 + 3
arithmetic operators
 
+ - * none / % ** + - * none / % none + - * / ?? % ??
integer division
 
$(( $a / $b )) $rem = $null
$quot = [Math]::DivRem($a, $b, [ref] $rem)
integer division by zero writes "division by 0" error message; statement terminates with a 1 status error: Attempted to divide by zero
float division
 
`echo " scale=5; $a / $b " | bc` $a / $b
float division by zero writes "division by 0" error message; statement terminates with a 1 status evaluates to Infinity which is not a float literal
power
 
[Math]::pow(2, 32)
sqrt
 
[Math]::sqrt(2)
sqrt -2
 
no sqrt evaluates to NaN which is not a float literal
transcendental functions e l s c none none none a none
how to use:
`echo 'e(2)' | bc -l`
[Math]::exp [Math]::log
[Math]::sin [Math]::cos [Math]::tan
[Math]::asin [Math]::acos [Math]::atan
[Math]::atan2
float truncation
round towards zero, round to nearest integer, round down, round up
none and no floats [Math]::truncate(3.14)
[Math]::round(3.14)
[Math]::floor(3.14)
[Math]::ceiling(3.14)
absolute value
and signum
[Math]::abs(-7)
[Math]::sign(-7)
integer overflow
 
modular arithmetic converts to float
float overflow
 
no floats evaluates to Infinity which is not a float literal
random integer, uniform float echo $RANDOM 15 bit integer random 100
random 1.0
seed random numbers RANDOM=17
r=$RANDOM
$r = random -setseed 17
bit operators
 
<< >> & | ^ ~ use with set /a:
<< >> & | ^ ~
none none -band -bor -bxor -bnot

# powershell 3.0:
-lsl -lsr
strings
posix shell cmd.exe powershell
string literal 'don'\''t say "no"'
"don't say \"no\""
$'don\'t say "no"'
none; barewords are used for strings

double quotes are used for filenames which contain spaces and other special characters. However, the double quotes are stored in variables and passed to commands.
'don''t say "no"'
"don't say `"no`""
newline in literal yes no; a bareword string can be continued on the following line by ending a line with ^; the CRLF is not part of the value of the string; not possible to store a CRLF in a variable yes
barewords yes yes yes
escapes in double quotes
\\ \"
in $' ' quotes:
\a \b \e \f \n \r \t \v \\ \' \cc \xhh \ooo
`' `" ``
`0 `a `b `f `n `r `t `v
in other backtick sequences the backtick is ignored
variable interpolation count=3
item=ball
"$count ${item}s"
$count = 3
$item = "ball"
"$count $($item)s"
length s="hello"
${#s}
$s = "hello"
$s.length
string comparison [ $USER = foo ]
[ $USER != foo ]
# case insensitive:
-eq -ne -gt -lt -ge -le

# case sensitive:
-ceq -cne -cgt -clt -cge -cle
index of substring none returns -1 if not found:
"foo bar".indexof("bar")
extract substring s="foo bar"
${s:4:3}
"foo bar".substring(4,3)
string concatenation c="hello, ""world" $c = "hello, " + "world"
string replication $hbar = "-" * 80
split none "foo,bar,baz" -split ","
join none @("foo","bar","baz") -join ","
sprintf `printf "tie: %s %d %f" "Spain" 13 3.7` $a = "Spain", 13, 3.7
"tie: {0} {1} {2}" -f $a
case manipulation echo "hello" | tr [a-z] [A-Z]
echo "HELLO" | tr [A-Z] [a-z]
A=hello
echo -n ${A:0:1} | tr [a-z] [A-Z]; echo -n ${A:1}
"hello".toupper()
"HELLO".tolower()
strip none " hello ".trim()
pad on right, pad on left `printf "%-10s" "hello"`
`printf "%10s" "hello"`
$s = "hello"
$s + " " * (10 - $s.length)
" " * (10 - $s.length) + $s
string to number A="12"
$(( 7 + $A ))

B=".037"
`echo 73.9 + $B | bc`
7 + "12"

73.9 + ".037"
number to string
 
all values are strings [convert]::tostring(7) + " items"

# or use variable interpolation
regular expressions
posix shell cmd.exe powershell
regex match s=hello
rx='[a-z][a-z]*'
if expr $s : $rx > /dev/null
then
  
fi
if ("hello" -match "^[a-z][a-z]*$") {
  
}
single substitution s='do re mi mi mi'
s=$(echo $s | sed s/mi/ma/)
global substitution s='do re mi mi mi'
s=$(echo $s | sed s/mi/ma/g)
$s = "do re mi mi mi"
$s = $s -replace "mi", "ma"
dates and time
posix shell cmd.exe powershell
sleep sleep 10
arrays
posix shell cmd.exe powershell
array literal nums=(1 2 3 4) set nums=(1 2 3 4) $nums = 1,2,3,4
$nums = @(1,2,3,4)
array size ${#nums[@]} set numsc=0
for %i in %nums% do set /a numsc+=1
echo %numsc%
$nums.Length
array lookup ${nums[0]} $nums[0]
array modification nums[1]=5 $nums[0] = 5
array slice ${nums[@]:1:2} $nums[1..2]
concatenation a=(1 2 3)
b=(4 5 6)
c=(${a[@]} ${b[@]})
@(1,2,3) + @(4,5,6)
manipulate back of array
manipulate front of array
array iteration for i in ${nums[@]}
do echo $i
done
foreach ($i in $nums) {
  write-host $i
}
sort $a = 3,2,4,1
$b = $a | Sort-Object
reverse $a = 1,2,3
[array]::reverse($a)
functions
posix shell cmd.exe powershell
function definition add() { echo $(( $1 + $2 )); }
or
function add { echo $(( $1 + $2 )); }
goto:eof
:add
  set /a sum=%~1+%~2
  echo %sum%
goto:eof
function add {
  param ($a, $b)
  $a + $b
}
function invocation add 1 2 call:add 1 2 add 1 2
missing argument value '' $null
extra arguments ignored ignored
default argument value none function add {
  param ($a=0, $b=0)
  $a + $b
}
named parameters none
variable number of arguments args in $1, $2, … with number of args in $#
return value return arg available in $? variable if a positive integer smaller than 256
lambda declaration none $x = { write-host "foo" }
lambda invocation none & $x
or
$x.invoke()
default scope global
nested function definition visible outside containing function
execution control
posix shell cmd.exe powershell
if if [ $n -eq 0 ]
then echo "no hits"
elif [ $n -eq 1 ]
then echo "1 hit"
else echo $n " hits"
fi
if %n% equ 0 (
  echo no hits
) else (
  if %n% equ 1 (
    echo one hit
  ) else (
    echo %n% hits
  )
)
if ($n -eq 0) {
  write-host "no hits"
} elseif ($n -eq 1) {
  write-host "one hit"
} else {
  write-host "$n hits"
}
while i=0
while [ $i -lt 10 ]
do i=$(($i + 1))
echo $i
done
set i=0
:loop
  set /a i+=1
  echo %i%
if %i% lss 10 goto :loop
$i = 0
while ($i -lt 10) {
  write-host (++$i)
}
break/continue break continue break continue
for for i in 1 2 3
do echo $i
done
set nums=(1 2 3 4)
for %%n in %nums% do echo %%n
for ($i=1; $i -le 3; $i++) {
  write-host $i
}
exceptions
posix shell cmd.exe powershell
raise exception return nonzero status throw "bam!"
catch exception trap 'echo "risky failed"' ERR
risky
try {
  throw "bam!"
}
catch {
  write-host "caught!"
}
uncaught exception behavior stderr and continue
files
posix shell cmd.exe powershell
print to standard out echo "hi world" write-host "hi world"
standard file handles /dev/stdin
/dev/stdout
/dev/stderr
read line `head -1 /etc/passwd`
read file `cat /tmp/a`
write to file echo "hello" > /tmp/a
append to file echo "hello" >> /tmp/a
directories
posix shell cmd.exe powershell
current directory
directory of script
processes and environment
posix shell cmd.exe powershell
external command ls dir dir
backticks s=`ls` $s=dir
command line args $1
$2


# pass args as $# words to twiddle
twiddle "$@"

# pass args as one word to twiddle
twiddle "$*"
%1
%2


rem no way to pass argument with space
twiddle %*
number of command line arguments $# set #=0
for %%i in %* do set /a #+=1
echo %#%
script name %0
speech depends on OS $sp = new-object -comobject "SAPI.SpVoice"
$sp.speak("I'm a PC")
environment variable shell variables are environment variables
$HOME
$env:home
command path which ping get-command ping
exit exit 0 rem batch script exits:
exit /b 0

rem also closes cmd.exe window:
exit 0
exit
set signal handler function int_handler {
  echo "exiting…";
  exit
}

trap int_handler INT
start job in background sleep 1000 &
suspend current job ^Z
list jobs jobs
background suspended job bg %1
bring background job into foreground fg %1
disown job disown %1
libraries and namespaces
posix shell cmd.exe powershell
library $ cat foo.sh
function add() {
  echo $(($1 + $2));
}
import library source foo.sh
add 3 7
library path none
library path environment variable none
namespace declaration none
namespace separator none
reflection
posix shell cmd.exe powershell
class
list defined variables printenv set get-variable
__________________________________________________________ __________________________________________________________ __________________________________________________________

General

version used

The version of the language used for verifying the examples in the reference sheet.

show version

How to get the version.

cmd.exe

The version number displayed at start up is the Windows kernel version. 6.3 is the Windows 8.1 kernel version number.

Grammar and Invocation

interpreter

The customary name of the interpreter and how to invoke it.

posix shell:

On Unix, scripts are executing by passing the file containing the script to the interpreter as an argument:

$ dash ~/configure.sh

If the executable bit is set, the file can be run directly:

$ ~/configure.sh

To determine the name of the interpreter that will process the script, Unix will look for the presence of a shebang (#!) at the start of the file. If the pathname to a command follows the shebang, it will be used to interpret the script. If no shebang is present, the script will be interpreted with /bin/sh which is bash on Mac OS X and Cygwin and dash on Ubuntu.

Command line arguments which set the positional parameters $1, $2, and so on can be set upon invocation as follows:

$ dash ~/configure.sh arg1 arg2
$ ~/configure.sh arg1 arg2

Arguments can also be put on the shebang line, but this is only useful for passing options to the shell. If the file foo.sh contains

#!/bin/sh -x

then invoking it as

./foo.sh

is equivalent to

/bin/sh -x foo.sh

Hardcoding a full path in a shebang is a common cause of portability problems because different systems may install the interpreter in different locations. The env command avoids the problem by searching the PATH directories for the command:

#!/usr/bin/env lua

powershell:

On Windows, a file is a PowerShell script if it has a .ps1 suffix. There is no need to mark the script as executable. However, PowerShell is not configured to run scripts by default. To change the configuration, start a PowerShell as an Administrator and run the following command:

set-executionpolicy remotesigned

It is possible to invoke a PowerShell script by specifying the PowerShell interpreter as the command and the script as an argument, but the suffix of the file must still be ps1:

powershell -file .\test.ps1

repl

How to invoke the REPL.

execute command and exit

How to pass a single command to be executed as a command line argument.

statement separator

How the parser determines the end of a statement.

posix shell:

A posix shell simple command consists of optional variable assignments, followed by a command and zero or more arguments and redirections. The command can be an external command, user defined function, or built-in.

A posix shell pipeline is a sequence of one or more simple commands joined by pipes |. The shell executes the commands in parallel and redirects the stdout of each command to the stdin of the following command. The exit status is the status of the last command.

The control operators ; & && || are pipeline separators. The semicolon ; enforces sequential execution. The ampersand & executes in parallel. The && executes to the first pipeline that returns a nonzero status. The || executes to the first pipeline that returns a zero status.

A list is one or more pipelines joined by control operators. A list can have a semicolon ; or ampersand & at the end. A list is terminated by a newline.

A newline does not terminate a list when:

  • inside single quotes '', double quotes "", backticks ``, or parens ()
  • inside a block started by the keywords: for, select, case, if, while, until

A newline that would normally terminate a statement can be escaped with a backslash.

Multiple lists can be grouped inside parens ( ) or curly brackets { }. When grouped inside parens, the lists are executed inside a subshell. The curly brackets, but not the parens, must be separated from their contents by white space. Also, within curly brackets, but not within parens, the last list must be terminated by a semicolon or newline.

word separators

escape special character

How to escape a special character.

block delimiters

How blocks are delimited.

posix shell:

Blocks can be delimited with {}, (), or the do,done keywords.

If a block is started with an open curly bracket {, then the block must be terminated with a line containing a close curly bracket by itself }.

If a block is delimited by (), then the commands in the block are executed in a subshell.

A block is delimited by do and done when using the execution control keywords for, select, while, and until.

The then and else keywords of an if statement start blocks which are terminated by a following elif, else, or fi.

The function and if keywords open blocks which are terminated by end keywords. The repeat keyword opens a block which is terminated by until.

to end-of-line comment

How to make the remainder of the line a comment.

multiline comment

How to comment out multiple lines.

posix shell:

The method described is the syntax for a here document, which is a multiline string literal.

Variables and Expressions

assignment

How to assign a value to a variable.

parallel assignment

How to assign values to variables in parallel.

swap

How to exchange the values held by two variables.

compound assignment operators: arithmetic, string, bit

The compound assignment operators for arithmetic, string, and bit operations

powershell:

Note that /= performs float division, even when both operands are integers.

When the left operand is a string, += concatenates the right operand to the left operand.

When the left operand is a string and the right operand an integer, *= concatenates the left operand with itself right operand times.

increment and decrement

The C-style increment and decrement operators which can be used in expressions.

variable declaration

How to declare a variable.

posix shell:

The following three lines have identical behavior:

A="hello, world"
declare A="hello, world"
typeset A="hello, world"

It is possible to make a read only variable. Again there are three ways:

readonly A="can't change"
declare -r A="can't change"
typeset -r A="can't change"

Variables are not exported to subprocesses unless declared to be exported:

export A="exported to subprocess"
declare -x A="exported to subprocess"
typeset -x A="exported to subprocess"

Variables assigned on the same line as a command are not created, only exported to the subprocess that instantiates the command:

EDITOR=emacs svn commit

By default variables defined inside functions are global. They can be declared to be local:

function foo () {
  local b=17
  # echoes 17:
  echo $b
}
# echoes nothing:
echo $b

are identifiers case sensitive?

powershell:

PowerShell identifiers are case insensitive.

null

The null literal.

null test

How to test if a value is null.

undefined variable access

The value of an undefined variable, or the system behavior if there is no such value.

undefined test

How to determine if a variable is undefined.

posix shell:

The expression ${var+val} is val if var is defined, otherwise it is the empty string ''.

The expression ${var:+val} is val if var is defined and not null, otherwise it is the empty string ''.

Arithmetic and Logic

posix shell:

The POSIX shell provides at least three different environments for logical expressions, each with their own operators and values for true and false.

Logical expressions are usually encountered in the conditionals of if, elif, while, and until. A command is expected as the conditional expression. The command is executed, and a return value of zero is treated as true and nonzero as false.

status codes [ ] $(( ))
where used command command argument
true true no canonical true value 1
false false '' 0
falsehoods nonzero exit status '' 0
logical operators && || ! -a -o ! && || !
grouping { } \( \) ( )
string relational operators none = != \< \> none
arithmetic relational operators none -eq -ne -lt -gt -le -ge == != < > <= >=
arithmetic operators none none + - * / % **
bit operators none none << >> & | ^ ~

posix shell: status codes:

Logical expressions can be formed using status codes returned by commands. The commands can be external, built-in, or user defined functions. A status code of zero is used to indicate success, and for the purpose of logic zero is treated as true and all other status codes as false.

The && and || operators are short circuit operators. An exclamation point ! can be used to negate the status code of a command. It is not necessary to separate && and || from their operands with whitespace, but it is necessary to mark off a ! used as negation with whitespace.

posix shell: test command:

posix shell: arithmetic expansion:

true and false

The literals for true and false.

falsehoods

Values which are false in conditional expressions.

logical operators

Logical and, or, and not.

posix shell:

&& || and ! are available inside [[ ]], (( )), and $(( )) expressions. Inside [ ] expressions use -a, -o, and !.

conditional expression

The syntax for a conditional expression.

are expressions statements

Whether an expression can be used where a statement is expected.

relational expressions

posix shell:

Bash has three types of relational expressions: [[ ]], [ ], and (( )). For a description of [ ], read the man page for test.

(( )) evaluates its contents in the same manner as the arithmetic expansion $(( )). If the result is zero, it returns 1 (false). Otherwise it returns 0 (true).

[[ $a == pattern ]] and [[ $a != pattern ]] interpret * and ? on the right side as patterns. Thus "hello" == "hell*" is true. For numeric comparison, use [ $a -eq num ] or [ $a -ne num ].

Bash expressions discusses the different types of bash expressions in more detail.

relational operators

posix shell:

If == and =! have an unquoted string on the right, then * and ? within the string will be treated as wild cards for matching.

convert from string

posix shell:

All values are strings. The $(( )) operator will interpolate any variables and then evaluate the resulting string as an arithmetic expression composed of integers. The variables are not limited to containing integers. The following script outputs 10:

A=7+3
echo $(($A))

To perform floating point arithmetic, bash must shell out to a floating point utility such as bc.

convert to string

arithmetic expressions

How to evaluate an arithmetic expression.

posix shell:

Bash arithmetic is available within $(( )) and (( )). The latter form evaluates the arithmetic expression and returns status 1 if the result zero, and 0 otherwise.

Bash only has integer arithmetic. For floating point arithmetic, use the external commands bc or dc.

arithmetic operators

The operators for addition, subtraction, multiplication, float division, integer division, modulus, and exponentiation. Some languages provide a function pow instead of an operator for exponentiation.

posix shell:

arithmetic operators are available in $(( )) and (( )).

integer division

How to perform integer division.

float division

How to perform floating point division, even if the operands might be integers.

posix shell:

The bash shell lacks built-in floating point arithmetic. bc is an arbitrary precision calculator, and scale is the number of digits to the right of the decimal point. If scale is not specified, it defaults to zero, which results in integer division.

It is also possible to use dc, which is a reverse polish notation arbitrary precision calculator:

`echo " 5 k $a $b / p " | dc`

arithmetic functions

Functions for computing square root, natural exponent, natural logarithm, sine, cosine, tangent, arcsine, arccosine, arctangent, and atan2.

The trigonometric functions are all in radians. atan2 takes two arguments which are the x and y co-ordinates of a vector in the Cartesian plane. It returns
the angle to the positive x-axis made by the vector.

arithmetic truncation

division by zero

integer overflow

float overflow

sqrt -2

The result of taking the square root of -2.

random integer, uniform float

The examples show how to generate a uniform random integer in the range from 0 to 99, inclusive; how to generate a uniform float in the range 0.0 to 1.0; how to generate a float from a standard normal distribution

posix shell:

$RANDOM evaluates to a random integer between 0 and 32767 inclusive.

seed random numbers

posix shell:

Bash 3.2.48 seeds the random number at start up using the current time and the PID:

  /* Seed the random number generator. */
  sbrand (dollar_dollar_pid + shell_start_time);

Here is the random number generation code:

/* A linear congruential random number generator based on the example
   one in the ANSI C standard.  This one isn't very good, but a more
   complicated one is overkill. */

/* Returns a pseudo-random number between 0 and 32767. */
static int
brand ()
{
  rseed = rseed * 1103515245 + 12345;
  return ((unsigned int)((rseed >> 16) & 32767));    /* was % 32768 */
}

powershell:

The initial seed is set to a value that varies each time PowerShell is started up.

If a repeatable sequence of random numbers is desired, the seed can be set to a specific value using the -setseed option on the first call to random.

bit operators

posix shell:

The bit operators are available in $(( )) and (( )).

Strings

string literal

The syntax for a string literal and how to escape the delimiter.

newline in literal

Whether a newline character sequence can be included in a string.

For all the languages described in this reference sheet a string literal is permitted to encompass multiple lines in the source code and the resulting string will contain the same number of lines.

escapes

Character escape sequences which can be used in string literals.

variable interpolation

How to interpolate variables in a string.

posix shell:

A dollar sign $ can be backslash escaped to prevent variable interpolation:

echo "the value of \$a is $a"

powershell:

A dollar sign $ can be backtick escaped to prevent variable interpolation:

write-host "the value of @@`@@$a is $a"

length

How to get the length of a string.

cmd.exe:

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

string comparison

How to determine if two strings are equal or unequal.

index substring

How to find the index of the start of a substring in a string.

extract substring

string concatenation

The string concatenation operator.

split

How to split a string into an array of strings.

powershell:

When splitting a string into words, no delimiter need be specified and the string to be split can follow the -split operator:

-split "foo bar baz"

join

How to concatenate the elements of an array into a string with a separator.

scan

sprintf

How to create a string using a printf style format.

case manipulation

strip

pad on right, pad on left

Regular Expressions

regex match

How to test whether a regular expression matches a string.

posix shell:

The double square bracket operator [[ ]] is not part of the POSIX standard but it is a feature of bash, ksh, and zsh. It supports a match test operator:

if [[ "hello" =~ ^[a-z][a-z]*$ ]]; then

single substitution

How to replace the first occurrence of a pattern in a string.

posix shell:

The following parameter expansion is not part of the POSIX standard but provided by bash, ksh, and zsh:

str='do re mi mi mi'
echo ${str/mi/ma}

global substitution

How to replace all occurrences of a pattern in a string.

posix shell:

The following parameter expansion is not part of the POSIX standard but provided by bash, ksh, and zsh:

str='do re mi mi mi'
echo ${str//mi/ma}

Dates and Time

Arrays

array literal

Array literal syntax.

array size

How to get the number of elements in an array.

array lookup

How to access a value in an array by index.

array slice

How to slice a subarray from an array.

array iteration

membership

How to test for membership in an array.

intersection

How to compute an intersection.

union

map

filter

reduce

universal predicate

How to test whether a condition holds for all members of an array. Always true for an empty array.

existential predicate

How to test whether an item in an array exists for which a condition holds. Always false for an empty array.

dictionary literal

dictionary size

dictionary lookup

dictionary iteration

out of bounds behavior

Functions

Python has both functions and methods. Ruby only has methods: functions defined at the top level are in fact methods on a special main object. Perl subroutines can be invoked with a function syntax or a method syntax.

function definition

posix shell:

A bash function definition can alternately be preceded by the keyword function, and when used, the parens following the function name are prohibited.

function invocation

posix shell:

The syntax for invoking a function is the same as the syntax for invoking a command. If a function is defined with the same name as a command in the search path, the function will be executed.

missing argument value

Value of an argument variable if a function is invoked with fewer arguments than are declared.

extra arguments

If a function is invoked with more arguments than are declared, how the function can access them.

default argument value

How to declare a default value for an argument.

variable number of arguments

How to write a function which accepts a variable number of argument.

named parameters

How to write a function which uses named parameters.

return value

posix shell:

Bash functions can only return small integers via return. However, a function can echo to stdout and the caller can invoke it with backticks to get a string value.

lambda declaration

How to define a lambda function.

lambda invocation

default scope

posix shell:

By default, bash and variables inside functions have global scope.

Execution Control

if

Some optional branching constructs:

posix shell:

case $a in (0) echo "no";; (1) echo "yes";; (2) echo "maybe";; (*) echo "error";; esac

while

posix shell:

Also has an until loop.

break/continue/redo

break exits a for or while loop immediately. continue goes to the next iteration of the loop. redo goes back to the beginning of the current iteration.

for

posix shell:

A C-style for loop:

for ((i=0; i<10; i++ )); do echo $i; done

Exceptions

raise exception

How to raise an exception.

catch exception

How to handle an exception.

uncaught exception behavior

System behavior if an exception goes uncaught. Most interpreters print the exception message to stderr and exit with a nonzero status.

posix shell:

The bash interpreter writes a message to stderr whenever a command returns a nonzero status. By default, the interpreter does not exit, but if this behavior is desired, then the following should be put at the top of the script:

trap exit ERR

wait on thread

Files

print to standard out

posix shell:

To prevent echo from appending a newline to the output, use

echo -n "hello"

standard filehandles

read line

read file

write to file

append to file

Directories

Processes and Environment

external command

posix shell:

The syntax for calling an external command is the same as the syntax for invoking a function. If a function is defined with the same name as an external command in the search path, the function is invoked.

backticks

command line args

speech

How to make the computer talk.

posix shell:

On Mac OSX the command say can also be executed from the bash or zsh prompt:

say "I'm a Mac"

On Ubuntu Linux this command can be used:

espeak "I'm Unix"

environment variable

command path

The directory containing a command. Also indicates if the command is a built-in, alias, or function. Shows the definition of aliases and functions.

exit

posix shell:

The exit status of a bash script is the return status of the last command executed, or the argument of exit.

set signal handler

Libraries and Namespaces

library

What a library looks like.

import library

library path

namespace declaration

namespace separator

Reflection

class

POSIX Shell

POSIX 2008: Shell & Utilities

Command Prompt

Command-line reference A-Z

PowerShell

Windows PowerShell User's Guide

issue tracker | content of this page licensed under creative commons attribution-sharealike 3.0