Skip to content

Instantly share code, notes, and snippets.

View rafaelmaeuer's full-sized avatar

Rafael M. rafaelmaeuer

View GitHub Profile
var sleepSetTimeout_ctrl;
function sleep(ms) {
clearInterval(sleepSetTimeout_ctrl);
return new Promise(resolve => sleepSetTimeout_ctrl = setTimeout(resolve, ms));
}
await sleep(<duration>);
const object = {a:2, b:4, c:6, d:8};
Object.entries(object).forEach(([key, value], index) => {
console.log(`${index}: ${key} = ${value}`);
});
<?php
$langs = array("PHP", "JavaScript", "Python", "C++", "Ruby");
$newLangsSpace = implode(" ", $langs);
$newLangsComma = implode(", ", $langs);
$newLangsHyphen = implode("-", $langs);
// Since we are printing a string, we can use echo to display the output in the browser
echo $newLangsSpace."<br>"."<br>";
echo $newLangsComma."<br>"."<br>";
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
<?php
if ($divisor == 0) {
// Other error-levels: E_USER_NOTICE, E_USER_WARNING
trigger_error("Cannot divide by zero", E_USER_ERROR);
}
?>
// Turns WordPress debugging on in wp-config.php
define('WP_DEBUG', true);
// Tells WordPress to log everything to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);
// Doesn’t force the PHP 'display_errors' variable to be on
define('WP_DEBUG_DISPLAY', false);
// Hides errors from being displayed on-screen

The tests you had listed :

  • Single Parenthesis - ( ... ) is creating a subshell
  • Double Parenthesis - (( ... )) is for arithmetic operation
  • Single Square Bracket - [ ... ] is the syntax for the POSIX test
  • Double Square Brackets - [[ ... ]] is the syntax for bash conditional expressions (similar to test but more powerful)

The single bracket [ is actually an alias for the test command, it's not syntax.

One of the downsides (of many) of the single bracket is that if one or more of the operands it is trying to evaluate return an empty string, it will complain that it was expecting two operands (binary). This is why you see people do [ x$foo = x$blah ], the x guarantees that the operand will never evaluate to an empty string.

The double bracket [[ ]], on the other hand, is syntax and is much more capable than [ ]. As you found out, it does not have the "missing operand" issue and it also allows for more C-like syntax with >, <, >=, <=, !=, ==, &&, || operators.

My recommendation is the following: If your interpreter is #!/bin/bash, then always use [[ ]]

It is important to note that [[ ]] is not supported by all POSIX shells, however many shells do support it such as zsh and ksh in addition to bash

General rule: quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.

$? doesn't need quotes since it's a numeric value. Whether $URL needs it depends on what you allow in there and whether you still want an argument if it's empty.

I tend to always quote strings just out of habit since it's safer that way.