Inspired by the incredibly useful rosetta stone for UNIX sysadmins. Also, see the awesome Hyperpolyglot site.
Language |
|
|
|
|
|
|
|
|
|
---|---|---|---|---|---|---|---|---|---|
Website |
|
|
|
|
|
|
|
|
|
Author |
|
|
|
|
|
|
|
|
|
License |
|
|
|
|
|
|
|
|
|
Single Line Invocation |
sh -c "script" |
ksh -c "script" |
csh -c 'script' |
awk 'script' |
perl -e 'script' |
echo 'script' | tclsh |
python -c script |
ruby -e 'script' |
|
Script File Invocation |
sh scriptfilename |
ksh scriptfilename |
csh scriptfilename |
awk -f scriptfilename |
perl scriptfilename |
tclsh scriptfilename |
python scriptfilename |
ruby scriptfilename |
|
Whitespace |
|
|
|
|
|
|
|
|
|
Statement Separator |
; or newline
|
; or newline
|
; or newline
|
; or newline
|
; |
; or newline
|
|
; or newline
|
; or newline
|
Block |
|
|
|
{ } |
{ } |
{ } and [ ] and even " " |
|
{ } ordo |
{ } |
Comment |
# |
# |
# |
# |
# |
# or ;# for end-of-line comments
|
|
# or =begin
|
// or /* */ |
Variable |
var when setting; ${var} or
$var otherwise
|
var when setting; ${var} or
$var otherwise
|
var when setting; ${var} or
$var otherwise
|
var |
$var |
var when setting; ${var} or $var otherwise
|
var |
var (local variable), @var (instance variable),@@var (class variable), $var (global variable), VAR (constant)
|
var x |
Array |
|
${array[n]} |
$wordlist[n] for a space separated string)
|
|
$array[n] or@array for whole
thing;$array[0] for first element
|
set list {a b c d} ; access with lindex list n (called "lists")
|
(1,2,3) tuple (immutable),[1,2,3] list (mutable)
|
var[n] |
array[n] |
Array Size |
|
${#array[*]} |
$#wordlist |
n=0;for(key in hash){n=n+1} and the largest key in the hash can be retrieved with: m=0;for(key in hash){if(m<key)m=key}
|
$#array + 1 |
llength list |
len sequence |
array.length |
array.length |
Hash |
|
|
|
hash[key] |
$hash{key} or%hash for whole
thing
|
hash(key) or array set hash {key value key value ... ...} (called "arrays")
|
{ key:value,..., key:value} |
var[key] |
object.key or object["key"] |
Hash Iterate |
|
|
|
for (key in hash) block |
foreach $key (keys %hash) |
foreach key [array names hash] |
for key,value in hash.items() |
|
for (key in object) |
String |
"quoted string" (with variable/command substitution),'quoted string' (without variable/command substitution)
|
"quoted string" (with variable/command substitution),'quoted string' (without variable/command substitution)
|
"quoted string" (with variable/command substitution),'quoted string' (without variable/command substitution)
|
"quoted string" |
"quoted string" (with variable/command substitution),'quoted string' (without variable/command substitution)
|
"quoted string" (with variable/command substitution, can include line breaks){quoted string} (without variable/command substitution, can include line breaks)
|
'Quoted string' "Quoted string" '''Quoted string including
|
'quoted string' or %q{quoted string} or"quoted string"
or %Q{quoted string} ;the latter two do substitution on #{code} |
"quoted string" or
'quoted string' |
Command line arguments |
$0 ,$1 ,$n where n
is $# (and only up to $9 ; after that, use shift )
|
$0 ,$1 ,$n where n
is $# (need form ${n} for n>9)
|
|
ARGV |
@ARGV |
argv list and argc variable
|
sys.argv |
ARGV |
|
Last Result |
|
$_ |
|
|
$_ |
|
_ (but only for explicit expressions in an interactive session)
|
$_ (deprecated)
|
|
Assignment |
var=value |
var=value |
set var = value |
var = value |
$var = value; |
set var value |
var = value |
= |
= |
Equality (string) |
= |
= |
== |
== |
eq |
== or eq |
== |
== |
== or === |
Equality (numeric) |
-eq |
-eq |
== |
== |
== |
== |
== |
== |
== or === |
Logical And |
-a |
-a or && |
&& |
&& |
&& |
&& |
and |
&& or and |
&& |
Logical Or |
-o |
-o or || |
|| |
|| |
|| |
|| |
or |
|| or or |
|| |
Logical Not |
! |
! |
|
! |
! |
! |
not |
! or not |
! |
Regexp Search |
|
|
|
/regexp/ or match(string,
regexp) |
/regexp/ |
regexp pattern string |
import re (for
repeated searches)or (for one-time matching) re.search(regexp, string)
|
string =~ /regexp/ |
string.match(/regexp/) |
Regexp Replace |
|
|
|
sub(regexp, replacestring, targetstring) |
s/regexp/replacement/ |
regsub pattern string replacestring resultvar |
result = reobj.sub(replacement, string) or result = re.sub(regexp, replacement, string) |
string.sub(/regexp/, replacementstring) orstring.gsub(/regexp/, replacementstring) |
|
Conditional |
if condition |
if condition |
if (expr) then |
if (condition) block |
if (test) {block} |
if {condition} block |
if condition: |
if expression [then|:] |
if (test) {block} |
Iteration |
for item in list |
for item in list |
foreach item (wordlist) |
for (var=lower; var <=upper; var++) block |
for ($i=1; $i<10; $i++) {block} for (10,9,8,7,6,5,4,3,2,1) {block} for (1..15) {block} foreach $element (@array) {block} |
for {set i 0} {$i <= 5} {incr i} block
|
for x in iterable: |
arrayvar.each do |var| or
for var in arrayvar
|
for (i=1; $i<10; $i++) {block} for (variable in object) {block}
|
Loop |
while condition |
while condition |
while (condition) |
while (condition) block |
while (test) {block} |
while {condition} block |
while condition: |
while expression
[do|:]
|
while (test) {block} |
Next Loop Iteration |
continue |
continue |
continue |
continue |
next |
continue |
continue |
next |
continue |
Exit Loop |
break |
break |
break |
break |
last |
break |
break |
break |
break |
Switch |
case value in |
case value in |
switch (string) |
|
|
switch $var { |
|
case or case target
|
switch (x) { |
Function Definition |
fnname () { commands; } |
fnname () { commands; } orfunction fnname { commands; } |
|
function fnname (paramlist) |
sub fnname {block} with arguments in
@_ |
proc fnname {arg1 arg2 ..} block |
def fnname(param1, param2,..): |
def fnname(param1,param2)
|
function fnname(arg1,arg2) {block} |
Function Call |
fnname |
fnname |
|
fn(param1, param2) |
&fnname(arg1, arg2) |
fn arg1 arg2 |
fn(arg1, arg2, ..) |
fnname(arg1, arg2) |
fnname(arg1, arg2) |
Function Parameters pass by |
|
|
|
|
|
upvar )
|
|
|
|
Print to stdout |
print
|
print |
echo |
print , printf |
print STDOUT "stuff to print\n" ,printf STDOUT format ... |
puts string |
print |
puts |
document.write |
Open File for Read |
|
|
|
|
open(FILEHANDLE, "filename") |
set f [open filename r] |
open(filename, mode='r') |
file = File.open("filename", "r") |
|
Open File for Write |
|
|
|
|
open(FILEHANDLE, ">filename") |
set f [open filename w] |
open(filename, mode='w') |
file = File.open("filename", "w") |
|
Read from File |
read (only from stdin )
|
read (-u n specifies file descriptor)
|
$< reads line from stdin |
|
<FILEHANDLE> |
gets $f var |
f.read() |
line = file.gets |
|
Write to File |
|
print -u n (n specifies file descriptor)
|
|
|
print FILEHANDLE "stuff to print\n" |
puts $f string |
f.write(string) |
file.puts |
|
Close File |
|
|
|
close(filename) |
close(FILEHANDLE) |
close $f |
f.close() |
file.close |
|
Test File |
test -r readable,test -f is plain
file,test -d is directory;[ condition ]
is alternative form
|
test -r readable,test -f is plain
file,test -d is directory; [ condition ]
is alternative form
|
-r readable,-e exists,-f is plain file,-d is directory
|
|
-r readable,-e exists,-f is plain file,-d is directory
|
file readable filename file exists filename file isfile filename file isdirectory filename |
os.path.exists(filename) |
File.readable?("filename") File.exist?("filename") File.file?("filename") File.directory?("dirname") |
|
Delete File |
rm filename |
rm filename |
rm filename |
|
unlink("filename") |
file delete filename |
os.remove(filename) |
File.delete("filename") |
|
Rename File |
mv oldfilename newfilename |
mv oldfilename newfilename |
mv oldfilename newfilename |
|
rename("oldname", "newname") |
file rename oldname newname |
os.rename(oldname, newname) |
File.rename("oldname", "newname"); |
|
File Info |
|
|
|
|
($dev, $ino, $mode, $nlink, $uid, |
file stat filename resultarray |
os.stat(filename) |
File.stat("filename") |
|
Output Formatting |
|
|
|
printf formats
|
printf formats
|
format |
|
printf |
|
String Concatenation |
|
|
|
"stringa" "stringb" |
"stringa" . "stringb" |
"$stringa$stringb" or append command
|
string1+string2 |
"stringa" + "stringb" |
"stringa" + "stringb" |
Substring |
${stringvar:offset:length} (bash only)
|
|
|
substr(string, offset [,length]) |
substr(string, offset [,length]) |
string range string offset last |
string[offset:endoffset] |
string[start, len] |
string.substring(startpos, endpos) |
String Tokenization |
|
|
|
split(string, array, [,separator]) |
split /separator/ expression |
split string separatorCharacters |
string.split(separator) |
string.split(/separator/) |
string.split(delimiter) |
Language |
|
|
|
|
|
|
|
|
|
Please let me know if you spot anything wrong or missing in the table.
Helpful references used in the compilation of this table include:
Thanks to:
Other links of interest:
This table is produced from a collection of XML source files using an XSLT stylesheet.