Sage-Code Laboratory

Bash Syntax

Bash is a curly bracket language. It uses curly brackets for functions and expressions. Unlike other languages, the curly brackets are not used for control statement blocks. Bash syntax is case sensitive. Most keywords are English reserved words but some abreviations or invented.

Hello World

This tutorial is based on code snippets. Next script is a simple app that print on the console the message "Hello World". From this example you can learn several syntax choices. This scrpt is saved with name: and it can be executed using Bash.
echo -n "hello "

function print {
  echo $1

print "world"
~/bash-repl$ bash
hello world


Script Demo

Let's glance a more sofisticated example. Read this example but be aware you will probably do not understand it. Is our job to explain the syntax and you will be able to read this code after you finish the tutorial
#list script files
for fhnd in *.sh
  printf '_%.0s' {1..30}
  echo; echo "#file: $fhnd"
  # display first 3 lines of code
  while read -r line; do
      echo $line
      if (( i == 3 )); then break; fi
      (( i++ ))
  done < $PWD/$fhnd
printf '=%.0s' {1..30}; echo

Critical Thinking

Bash syntax try to be punk and original but it breaks the Engineering design principles: To be consistent, to be easy to learn, to be "foolprove". Also Bash fail the KISS principle. In our opinion Bash should be forgoten and someone should use another language to create a better operating system based on a new language.

End of statement problems

Bash has optional semicolon but this is required in some cases where it should not be. Becouse of this, Bash needs continuation operator "\" at end of line, to tell the interpreter that a command is not finished and continue on the next line.

String problems

Symbols and strings are not conceptual different. This is very confusing. The parser is detecting a word and can't decide if is a string or a command. So the error message is wrong many times. It says: invalid command when in fact it should interpret the arguments like strings. So you better put all your substitutions and literals in double quotes.

Space problems

In Bash, the assign expression "=" and the variable must stick together. If you add a space like is the convention in most programming languages, you get an error that does not make sense. Well get use to it because this is happening often. Sometimes you need a space separator sometimes you don't depending on the context.

Anoyng symbols

Bash is using same symbol for too many things. For example {...} are used for blocks of code but also for ranges, string expansion, parameters and array elements. Round paranthesis (...) are used for declaring arrays and command groups but also for arithmetic expressions and arithmetic expansion. If you use it wrong you may end-up with error messages that drive you creasy until you learn how to do it right.

Lection learned

There is other much better language inspired from Bash. This language "PHP" is fixing many of Bash problems but inherite a similar design. Therefore Bash and PHP both are in decline. New languages have learned the lection: How not do make a programming language. Python, Ruby, Perl and recently Julia are 4 other languages competing with Bash for shell scripting.

Read next: Shell Console