shell programming - variables

created onApril 18, 2022

variable names

Variable names are case sensitive. They have to start with a lower or an upper case letter or an underscore.

Do not use all upper case variable names. The general convention is that all upper case variables (with underscores) are reserved for environment variables. Nevertheless, there are plenty of shell scripts in the wild that use all upper case variable names.

variable declaration

general form a variable declaration:

<variable name>=<variable value>
Do not use blanks around the equals sign. Doing so will result in the error message <variable name>: command not found.

read-only variables

Read-only variables can be declared with , i.e.:

declare -r someString="here we go"

integer variables

The shell does not have strict typing. However, integer variables can be declared with , i.e.:

declare -i someInt=42
If a string value is accidently assigned to an integer variable, the value will be accepted silently and the integer variable will have the value of 0 (zero).

referencing variables

Variables are referenced by prepending the variable with a and wrapping the variable name in curly braces:

printf 'username %s\n' ${username}

One can omit the curly braces in most situations, but using the curly braces when referencing variables safeguards against a lot of errors, i.e.:

  • if you want to reference the variable space in the string “${space}ball”, since “$spaceball” would instead reference the variable identified by spaceball.
  • it is easy to run into errors like using without realizing that the underscore becomes part of the first variable name. Quoting the first variable would work, too, and needs to be done anyway regardless of curly braces being used, if the first variable contains blank, but the curly brace don’t hurt.

Curly braces are mandatory in the following cases:

  • referencing array elements, i.e.: ${myArray[7]}
  • referencing positional parameters beyond 9: $8 and $9 work perfectly, $10, $11, do not work, you have to use ${10}, ${11}, …
  • using parameter expansion operations, i.e. ${filename%.*} for removing a filename extension.

If the variable contains blanks, it should be quoted. If a variable with blanks is given as an argument to a command and the variable is not quoted, the command will process the variable not as a single argument but will process the veriable as a number of arguments, separated by the blanks in the variable.

In the example below, the shell takes the variabe sentence as a number of string arguments, separated by blanks. printf is invoked for each of these arguments, which is why every word in the variable sentence is printed line by line:

$ sentence="usally, sentences consist of several words." $ printf '%s\n' ${sentence} usally, sentences consist of several words.

If the variable is quoted, the shell takes the variables as one argument, and the variable is printed on a single line as one might expect:

$ sentence="usally, sentences consist of several words." $ printf '%s\n' "${sentence}" usally, sentences consist of several words.

The best habbit is wrap all variable names in curly braces and to quote all variables unless you are sure that a variable does not contain blanks.

special shell script variables

in a shell script you have access to some special shell script variables:

  • is the path of shell script file. With you get the the shell script name.
  • is an array of the arguments (options), with which the shell script has been invoked.
  • is a string that contains all arguments, separated by a blank.
  • is the number of arguments provided to the sccript.
  • to denote the argument at position 1 to n for a number of n arguments. is the first argument, is the second argument and so on.
  • in the shell, is the PID (process ID) of the shell. In a shell script, is the PID of the shell script.

the following special shell variables have the same meaning as in the shell:

  • is the exit status of the last command

shifting the arguments array with shift

The array , which contains the shell script arguments, can be shifted left with . For each invocation of shift, the array values are shifted left one position (or index). Note, that the array of arguments always starts at index 1. is not modified by and retains its original value.

The following example illustrates this. running the script with the following content:

#!/bin/bash printf 'args: ' printf "'%s' " "${@}" shift printf '\nargs: ' printf "'%s' " "${@}" printf '\narg[0]: %s\n' "${0}"

with the arguments ‘so’ ‘what’ results in the following output:

rudolf@idoru:~/src/shell$ ./shift1 so what args: 'so' 'what' args: 'what' arg[0]: ./shift1 rudolf@idoru:~/src/shell$

environment variables

shell scripts have access to all environment variables, that is, all the variables you get listed when running the command in a shell. A shell script can export environment variables only to commands and child processes which the shell scripts initiates. A shell script invoked from the command line cannot export variables back to the command line environment.