top of page

Bash Variables: Defining and Unsetting

  • Writer: compnomics
    compnomics
  • Mar 17
  • 2 min read

Variables are fundamental to scripting in Bash, allowing you to store and manipulate data. This post will guide you through defining and unsetting variables, empowering you to create more dynamic and efficient scripts.


Defining Variables:

In Bash, you define variables using the following syntax:

  • variable_name=value

  • Important: There should be no spaces around the equals sign (=).

  • Example: my_variable="Hello, Bash!"


Variable Types (Implicit):

Bash variables are typeless; they store strings by default. However, you can perform arithmetic operations on variables containing numbers.


Accessing Variables:

You access a variable's value using the dollar sign ($) followed by the variable name:

  • echo $my_variable

  • echo ${my_variable} (braces are used for clarity, especially when concatenating variables)


Unsetting Variables:

You remove a variable and its value using the unset command:

  • unset my_variable


Environment Variables:

Environment variables are variables available to all processes. They are often defined in your .bashrc or .bash_profile files.

  • export variable_name=value (makes a variable an environment variable)


Practical Examples:

  1. Defining a Variable:

name="John Doe"
  1. Accessing a Variable:

echo "My name is: $name"
  1. Unsetting a Variable:

unset name
  1. Exporting an Environment Variable:

export MY_PATH="/usr/local/bin"

Practice Session 1: With Example Commands

  1. Define a variable named greeting with the value "Welcome to Bash!".

greeting="Welcome to Bash!"
  1. Display the value of greeting.

echo $greeting
  1. Define a variable named count with the value 10.

count=10
  1. Display the value of count inside of a string.

echo "The count is: ${count}"
  1. Unset the greeting variable.

unset greeting
  1. Try to display the value of greeting again to verify it has been unset.

echo $greeting
  1. Create an environment variable called MY_TEST_VAR with the value "Test Value".

export MY_TEST_VAR="Test Value"
  1. Display the value of MY_TEST_VAR.

echo $MY_TEST_VAR

Practice Session 2: Without Example Commands

  1. Define a variable named user_name with your name.

  2. Display a message that includes the value of user_name.

  3. Define a variable named file_path with a directory path.

  4. Display the value of file_path.

  5. Unset the user_name variable.

  6. Attempt to display the value of user_name to confirm it's unset.

  7. Create an environment variable named MY_APP_HOME with a path to an application's installation directory.

  8. Display the value of MY_APP_HOME.

  9. Create a variable called message, and then display the variable message with some appended text.

  10. Create a variable called number, and then display the variable number added to the number 5.

By mastering variable definition and unsetting, you can create more robust and adaptable Bash scripts.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page