Skip to content

Instantly share code, notes, and snippets.

View full-sized avatar

Rafael M. rafaelmaeuer

View GitHub Profile
View bash-brackets-conditionals.md

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)
View bracket-syntax-bash.md

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

View quote-shell-var.md

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.

View docker-compose-m1.sh
if [[ $(arch) == 'arm64' ]]; then
export DOCKER_DEFAULT_PLATFORM=linux/amd64
fi
View apple-script-admin.scpt
do shell script "command" user name "username" password "pass" with administrator privileges
View check-var-empty.sh
# -n operator: check if var is not empty
VAR="test"
if [ -n "$VAR" ]; then
echo "$VAR is not empty"
fi
# -z operator: check if var is empty
VAR=""
if [ -z "$VAR" ]; then
echo "$VAR is empty"
View count-remove-oldest.sh
# The script receives the limit as an argument.
limit=$1
number_of_files=$(ls ./files | wc -l)
if [ $number_of_files -gt $limit ]
then
# There are more files than the limit
# So we need to remove the older ones.
cd files
View shell-script-args.sh
wrappedProgram "$@"
# ^^^ this is correct and will hand over all arguments in the way
# we received them, i. e. as several arguments, each of them
# containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
# original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
# will then split the string as the shell does on the command