I like to write tutorials when I learn to solve a problem with Python, Bash, Git, Thunderbird mail or other areas. It is a useful reference when I need to solve the problem again.

I aim to get at least beginner level experience in a variety of languages, for flexibility and perspective. I enjoy writing a few scripts or tutorials to consolidate what I’ve learned. In that repo, I include instructions on how to get started, such as running a C, PHP or Ruby script for the first time.

From reading about the C programming language, which first appeared in 1972, I was amazed to find that so many sources say it is pervasive, powerful and quick. It is a low-level language that is close to machine code (which is one step away from binary), which means you can use C to do manage the machine precisely and efficiently. It is used behind-the-scenes to implement many coding languages, including Java, Python, iOS, Bash and SQL to name a few. Languages influence each other, but I find it fascinating to go a common root, to see where certain conventions come from, or where there was something missing which could be added in a newer language. In C for example there are functions but no classes, memory is not garbage collected it is in Java or Python and integer values for 1 and 0 are typically used in place of True and False because there is no boolean type (at least in the original C standard).

Strings and characters

String data types differently across languages.

In Java, double-quotes is a String type while single-quotes is a char type.

In Python, the double-quoted "H" and single-quoted 'H' are equivalent and both a create a string.

I was surprised to find that in C there is no string data type. Though, double-quotes allows creation of an array of characters.

void main()
{
    char one_letter = 'H';
    char one_letter_alt[1] = "H";
    char many_letters[5] = "Hello";
}

Substitution

In Bash, the double-quotes allows substitution of variables, while single-quotes restricts to literal characters.

$ NAME='Michael'
$ echo Hello $NAME!
Hello Michael!
$ echo "Hello $NAME!"
Hello Michael!
$ echo 'Hello $NAME!'
Hello $NAME!

This is similar in PHP.

$ php -a
php > $name = 'Michael';
php > echo 'Hello ' . $name . '!';
Hello Michael!
php > echo "Hello $name!";
Hello Michael!
php > echo 'Hello $name!';
Hello $name!

Bash

In Bash, double quotes around a variable can give different output compared to without.

$ echo $HOME
/home/michael
$ echo "$HOME" # Same output as above.
/home/michael
$ echo ~
/home/michael
$ echo "~" # Will not expand.
~

C

Interpolating (or substituting) values into a string in C and Python is also very similar.

// C
#include <stdio.h>

void main()
{
    char greeting[5] = "Hello";
    int number = 1;
    printf("string: %s. decimal: %d\n", greeting, number);
    // string: Hello. decimal: 1
}

Python

$ python
>>> greeting = "Hello"
>>> number = 123
>>> "string: %s. decimal: %d" % (greeting, number)
>>> string: Hello. decimal: 123

Though, the newer Python standard (for versions 2.7+ and 3.2+) is to use the curly brackets as the format method.

$ python
>>> string_value = "Hello"
>>> decimal_value = 123
>>> "string: {0}. decimal: {1}".format(string_value, decimal_value)
string: Hello. decimal: 123
>>> # OR
>>> "string: {greeting}. decimal: {number}".format(greeting=string_value,
                                                   number=decimal_value)
string: Hello. decimal: 123

From Python version 3.6, you can use the much cleaner f strings, which implicitly substitutes in values which are in scope, provide the string has a f prefix.

$ python3.6
>>> greeting = "Hello"
>>> number = 123
>>> f"string: {greeting}. decimal: {number}"
string: Hello. decimal: 123

Ruby

Ruby supports the implicit interpolation too, but some years before Python did.

$ irb
irb> greeting = "Hello"
irb> number = 123
irb> puts "string: #{greeting}. decimal: #{number}"
string: Hello. decimal: 123

I’ve noted that languages borrow from each other, especially from older languages.

Examples of printing

Bash

# bash
$ printf 'Foo'
$ sprintf 'Foo'
$ echo 'Foo'

See printf notes and What is the difference between printf, sprintf and fprintf?

PHP

print 'Foo';
print('Foo');
echo 'Foo';
echo('Foo';)

See also the print_r function.

Python

# python2
print 'Foo'
# python3
print('Foo')

Ruby

puts 'Foo'
print 'Foo'

C

Newline characters must be explicitly set.

putchar('F');
puts("\n");

puts("Foo\n");

printf("%s %d\n", "Foo", 100);

See puts for print a string and putchar to print a character.

Incrementing

The increment notation of i++; is in Java and JavaScript. It actually goes back at least as far as C. In Python, you can just do i += 1.

Line termination

Bash

A semicolon is not required in general use, but it can be useful for executing multiple commands in one line.

$ echo 'Line 1'
Line 1
$ echo 'Line 2'
Line 2
$ echo 'Line 1'; echo 'Line 2'
Line 1
Line 2

A semi-colon can also be useful for doing an if or for conditional in a single line.

For example, check if a value is greater than another.

$ if [ 3 -gt 2 ]; then echo 'Yes'; else echo 'No'; fi```
Yes

The longer way is with line breaks instead of semi-colons.

$ if [ 3 -gt 2 ]
> then echo 'Yes'
> else echo 'No'
> fi
Yes

In a script, this would be:

if [ 3 -gt 2 ]
  then
    echo 'Yes'
  else
    echo 'No'
fi

PHP

$x = 1;

Python

A newline character is used in Python. A semi-colon is optional but discouraged.

x = 1
y = 2;

Though, it can be useful for writing a single line of multiple statements.

x = 1
y = 2
print(x + y)
x = 1; y = 2; print(x + y);

And also useful running multiple Python commands with a single string argument in Bash.

$ python3 -c 'x = 1; y = 2; print(x + y)'
3

Line breaks also work for that single string argument.

$ python3 -c 'x = 1
> y = 2
> print(x + y)
> '
3

JavaScript

The semi-colon is JavaScript is required when in strict mode, but otherwise it can be inferred in some cases. Though, this is not recommended because it can be ambiguous.

"use strict";
var x = 1;
var y = 2;

Functions

There are different approaches to creating functions in each language.

Python uses def my_func():.

JavaScript uses function my_func() {}.

A Bash function accepts either my_func() {} or function my_func {}.

While a C function has no function keyword but is in the format return_type my_func {}.