Chapter 2: Variables

Quotations

  • Single Quotes: Bash will treat as literal text, it will not attempt any substitutions, replacements etc.
  • Double Quotes: When you wrap up text in double quotes, bash will still interpret substitutions, expansions, evaluations,variables, and so on, but it will be reasonable about not trying to do that when it doesn’t it make sense to.

Setting Variables


Syntax to set variables: var_name=value

  • Make sure there is NO space around the assignment operator.
  • Use lowercase for variable names, to distinguish them from environment variables.
  • If the value has spaces in it, enclose it in double quotes.
  • Variable names are case sensitive.
  • Variable name in bash must begin with a letter or an underscore, and can be followed by any letter or number, or more underscores.
  • Variables can be changed, re-assigned during the course of the script.

Specifying Default Values

If users do not provide the required inputs, we can provide default values to variables:

1 # 2nd positional parameter is password
2 # If user doesn't provide the value, then Password1 is de\
3 fault
4 user_name="$1"
5 user_password="${2:-Password1}"

Reading Variables


Syntax to read variables: "$var_name"

Best practice is to quote the variables - this is to protect elements like spaces within our variables.

If you expand a variable that doesn’t exist, Bash won’t issue any warnings and just replace the variable with an empty string. If you want Bash to throw an error whenever an unset variable is expanded, use set -u

To Brace or Not to Brace?


If we need to push other characters against our variable name, use braces "${var_name}"
If you need to distinguish the variable name from characters around it, you can use curly braces, { } - they are optional, but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

In the below example, since the variable name was not delimited, the _ character was considered part of the variable name - the shell tried to expand the non-existent $a_, which does not exist and therefore nothing was returned. Wrapping the variable with curly braces solves this problem:

1 a="hello"
2 echo $a_
3 # will print a blank line
4 echo ${a}_
5 # will print hello_

We will use braces for most parameter expansions, because if we left them off, the shell would just interpret the part after the parameter name, as characters and show them instead of using them to do what we intend.

Built-in Variables

  • command -V echo will show that echo is a built-in, which will take precedence over other commands.
  • enable -n echo will run command version (/bin/echo) of echo instead. I can re-enable it with enable echo
  • enable -n will show the builtins that are disabled
  • echo string will output the string with a newline. This is the builtin version. I can mention builtin echo string to specify that I want to run builtin version, or command echo string for command version.
  • Builtins use a different documentation that regular man pages. There’s a builtin called help that shows supporting information about builtins. Example, help echo
  • help will show a list of all builtins.

Environment Variables

Variable assignments as done above are only visible in the current shell - their values don’t affect a program called by a script (i.e. a forked process)

If you want a way for variables to be visible and usable by any of the programs you call on the script or command line, you need to use environment variables using the export builtin command.

1 $ myvar="John"
2 $ export myvar
3 $ declare -p myvar
4 declare -x myvar="John"
  • The -x in the output tells that the variable has been exported to the environment.
  • You can use declare -x to get a list of all environment variables and their values.
  • We can also use env and will need to sort its output to read it.

You should only export variables when you actually need to use their values in subprocesses - you risk overwriting an important environment variable used by a program without intending to, which can be hard to debug.

declare Command

declare is a shell built-in, and is used to declare shell variables and functions, set their attributes and display their values.

1 declare -- today='August 9th'

The -- in above example tells that there’s no special attribute associated with the variable

  • declare -r myvar=value will make the variable read only and cannot be changed later on.

Below options allow us to turn the characters in the value to lower or uppercase. Even when the value of the variable is changed later on, it will be transformed to lower or uppercase. These help us normalize user inputs for consistence.

  • declare -l myvar=value will turn the characters in the value to lowercase.
  • declare -u myvar=value will turn the characters in the value to uppercase.

declare -p will show all variables set in the current session.