bash: let

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Documented in the 'SHELL BUILTIN COMMANDS' section of man bash or here:
http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html

#!/bin/bash
loaded="configuration_file"

# The following will not work because the script will try to run the non-existing command 'configuration_file'
# $loaded=1
# However, this works:
let $loaded=1

For an alphanumeric string, use declare:

  # -g makes the variable global.
  declare -g $1=$2

Errors

Do not add spaces, as the following errors show:

$ i=10
$ let i = i + 1
bash: let: =: syntax error: operand expected (error token is "=")
$ let i= i + 1
bash: let: i=: syntax error: operand expected (error token is "=")
$ let i=i + 1
bash: let: +: syntax error: operand expected (error token is "+")
$ let i=i+1
$ echo $i
11