Technical skill

Bash syntax: Shell Variables

Give a value a name

A Bash variable is a named place to keep a value while a shell session runs. If a script uses the same file path several times, a variable lets you store it once and refer to it by name. Bash is the command interpreter that reads the commands. Assignment has no spaces around the equals sign:

filename="daily report.txt"

Use $filename to expand the value, meaning to replace the name with its contents. Double quotes around an expansion keep the result as one argument even when it contains spaces. They also stop wildcard characters in the value from expanding into filenames. For ordinary command arguments, write "$filename" unless you have a specific reason to request word splitting or pathname expansion.

Choose a default deliberately

An unset variable has no value defined; an empty variable is defined but holds no characters. Parameter expansion can supply a fallback. For example, ${output_dir:-./out} uses ./out when output_dir is unset or empty. The related forms without the colon test only whether a variable is unset, and forms with = can assign the default back to it. Choose the form deliberately when an empty value has meaning in the script.

A variable created in Bash is available in that shell, but Bash does not automatically include it in the environment passed to external commands. export marks it for inheritance:

LANGUAGE="en"
export LANGUAGE
printf 'language: %s\n' "$LANGUAGE"

A child process receives the environment when it starts. If it changes its own environment variables, those changes do not flow back to the parent shell. A builtin run directly in the shell can read its ordinary variables; an external program sees exported ones. The env command can inspect or construct the environment for another command. Avoid printing environments into shared logs because they may contain credentials or tokens.

Keep names and arguments intact

Bash variable names are case-sensitive. Bash identifiers may use letters, digits, and the _ character, but cannot begin with a digit. Lowercase names are common for local shell variables and uppercase names for environment variables; this is a convention, not a rule the shell enforces. unset name removes a variable from the current shell. A subshell starts with a copy of the parent's shell state, and its later assignments do not change the parent.

For a function that forwards its arguments to another command, "$@" expands to one word for each original argument and keeps their boundaries. That is usually safer than "$*" or unquoted $@:

run_tool() {
  command "$@"
}

If the caller runs run_tool "daily report.txt", the function passes one argument containing the space. Avoid using eval to turn variable contents into syntax. It asks Bash to parse the text again, which can convert data into commands.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful