Bash Script Variables

  • Read
  • Discuss

In bash scripting, a variable is a way to store and manipulate data. Bash script variables are declared using the = sign, with no spaces around it. Follow the following steps to get comfortable with bash script variables

  1. Open your terminal or shell.
  2. Create a new file using a text editor of your choice. For example, you can use the nano editor to create a file called “variables.sh”:
nano variables.sh
  1. Type the following code into the file:
#!/bin/bash# Declaring a variable in Bash
name=”Ali”

# Accessing the value of a variable
echo "Hello, my name is $name"

chmod +x variables.sh

./variables.sh

You should see the message “Hello, my name is Ali” printed in the terminal.

Echo Variable in terminal

To print the value of a variable, we can use the echo command in the terminal. For example:

echo “hello Wrold”

And that’s it! This is a simple example of declaring and using variables in a Bash script. You can use variables to store values, such as strings, numbers, or even the output of commands, and use them in various ways in your scripts. 

Leave a Reply

Scroll to Top