How to capitalize Bash and shell script variable?

Bash and shell script variable capitalization?

In Bash and other shell scripts, variable names are case-sensitive. This means that myVariable and MYVARIABLE are two distinct variables.

By convention, variable names in Bash are written in lowercase or with underscores (_) to separate words. The use of uppercase letters is generally avoided, except in a few cases. Here are some guidelines for capitalization in Bash variable names:

  • Use lowercase letters for most variable names.
  • Use underscores (_) to separate multiple words in a variable name.
  • Use uppercase letters for global variables, which are variables that are used throughout a script or multiple scripts.
  • Use uppercase letters for Bash built-in variables, such as $HOME, $PATH, and $IFS.

Here are some examples:


# lowercase variable

# lowercase variable with underscores

# global variable (uppercase)
MY_GLOBAL_VAR="hello world"

# Bash built-in variable (uppercase)
echo "My home directory is $HOME"

It's generally a good idea to follow these naming conventions to make your code more readable and easier to understand, both for yourself and others who may read it later.

  1. Dynamic variable names in Bash
  2. How to parse command line arguments in Bash?
  3. How to get the directory where a Bash script is located from within the script itself?
  4. Parsing JSON with Unix tools
  5. Command not found error in Bash variable assignment
  6. How to set a variable to the output of a command in Bash?
  7. How to run Bash commands in Python?
  8. Extract filename and extension in Bash?
  9. Looping through the content of a file in Bash