w3resource

Bash Variables: Declaration, usage and explanation

1.

Simple Variable Declaration:

Write a Bash script that declares a variable named "name" and assign it the value " Marcela". Print the value of the variable to the terminal.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Variable declaration and assignment
name="Marcela"

# Printing the value of the variable
echo "The value of the variable 'name' is: $name"

Output:

The value of the variable 'name' is: Marcela

Explanation:

In the exercise above,

  • name="Marcela": This line declares a variable named 'name' and assigns it the value "Marcela". In Bash, variables are declared and assigned values without specific data type.
  • echo "The value of the variable 'name' is: $name": This line uses the "echo" command to print a message to the terminal. The message includes the text "The value of the variable 'name' is:" followed by the value of the 'name' variable. The variable's value is accessed using the '$name' syntax, which substitutes the variable's value into the string.

2.

Variable Interpolation:

Write a Bash script that declares two variables, "firstName" and "lastName", and assign them your first name and last name, respectively. Print a message greeting yourself using variable interpolation.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Variable declaration and assignment
firstName="Dobrilo"
lastName="Ingalill"

# Printing a message greeting yourself using variable interpolation
echo "Hello, $firstName $lastName! Nice to meet you."

Output:

Hello, Dobrilo Ingalill! Nice to meet you. 

Explanation:

In the exercise above,

  • The 'firstName' variable is declared and assigned the value "Dobrilo".
  • The 'lastName' variable is declared and assigned the value "Ingalill".
  • The "echo" command prints a message to the terminal. Variable interpolation is used to include the values of the 'firstName' and 'lastName' variables in the message.
  • When the script is executed, it prints a message greeting you with your first name and last name.

3.

Numeric Variables:

Write a Bash script that declares a variable named "age" and assign it your age. Print a message including your age.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Variable declaration and assignment
age=20

# Printing a message including your age
echo "I am $age years old."

Output:

I am 20 years old.

Explanation:

In the exercise above,

  • The 'age' variable is declared and assigned the value 20.
  • The "echo" command prints a message to the terminal. Variable interpolation is used to include the value of the 'age' variable in the message.
  • When the script is executed, it prints a message indicating your age.

4.

User Input:

Write a Bash script that prompts the user to input their favorite mobile. Store the input in a variable named "mobile" and display a message including their favorite mobile.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Prompting the user to enter their favorite mobile
echo "Please input your favorite mobile:"

# Reading user input and storing it in a variable named "mobile"
read mobile

# Printing a message including the user's favorite mobile 
echo "My favorite mobile is $mobile."

Output:

Please input your favorite mobile:
Apple
My favorite mobile is Apple.

Explanation:

In the exercise above,

  • The "echo" command prompts the user to input their favorite mobile device.
  • The "read" command reads user input from the terminal and stores it in a variable named 'mobile'.
  • The "echo" command then prints a message including the user's favorite mobile using variable interpolation.

5.

Variable Concatenation:

Write a Bash script that declares two variables, "var1" and "var2", and assign them two different words. Concatenate the variables and print the result.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Variable declaration and assignment
var1="Hello"
var2="World"

# Concatenating the variables and printing the result
echo "$var1 $var2"

Output:

Hello World

Explanation:

In the exercise above,

  • The 'var1' variable is declared and assigned the value "Hello".
  • The 'var2' variable is declared and assigned the value "World".
  • The "echo" command prints the concatenated values of 'var1' and 'var2', separated by a space.
  • When the script is executed, it will print "Hello World", which is the result of concatenating the values of 'var1' and 'var2'.

6.

Variable Reassignment:

Write a Bash script that declares a variable named "x" and assign it a numeric value. Then, reassign it to a different value and print the updated value.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Variable declaration and initial assignment
x=100

# Reassigning the variable to a different value
x=200

# Printing the updated value of the variable
echo "The updated value of x is: $x"

Output:

The updated value of x is: 200

Explanation:

In the exercise above,

  • The 'x' variable is declared and initially assigned the value 100.
  • The value of 'x' is then reassigned to 200.
  • The "echo" command prints a message to the terminal, including the updated value of the 'x' variable.
  • When the script is executed, it will print "The updated value of x is: 10", indicating the new value of 'x'.

7.

Variable Scope:

Write a Bash script with a variable declared inside a function. Try to access the variable outside the function and observe the result.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Define a function
my_function() {
    local inside_variable="This is inside the function"
    echo "Variable inside function: $inside_variable"
}

# Call the function
my_function

# Attempt to access the variable outside the function
echo "Variable outside function: $inside_variable"

Output:

Variable inside function: This is inside the function 
Variable outside function:

Explanation:

In the exercise above,

  • Define a function named "my_function".
  • Inside the function, we declare a local variable named 'inside_variable' and assign it a value.
  • We then call the function "my_function", which prints the value of 'inside_variable' inside the function.
  • Outside the function, we try to access the variable 'inside_variable' and print its value.
  • However, since 'inside_variable' is declared as a local variable inside the function, it is not accessible outside the function.

8.

Command Output to Variable:

Write a Bash script that uses command substitution to store the output of the datetime command in a variable named "currentDateTime". Print the value of "currentDateTime".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Storing the output of the date command in a variable
currentDateTime=$(date)

# Printing the value of the variable
echo "Current date and time: $currentDateTime"

Output:

Current date and time: Fri 12 Apr 2022 12:46:32 PM UTC

Explanation:

In the exercise above,

  • The "date" command is used to display the current date and time.
  • Command substitution $(...) is used to capture the output of the "date" command and store it in the variable 'currentDateTime'.
  • The "echo" command prints the value of the variable 'currentDateTime', which contains the current date and time.

9.

Array Declaration:

Write a Bash script that declares an array named "colors" containing the names of your favorite colors. Print the entire array.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Declare an array named "colors" containing favorite colors
colors=("Red", "Blue" "Green" "Purple" "Yellow")

# Print the entire array
echo "My favorite colors are: ${colors[@]}"

Output:

My favorite colors are: Red, Blue Green Purple Yellow

Explanation:

In the exercise above,

  • An array named 'colors' is declared and initialized with the names of favorite colors.
  • The ${colors[@]} syntax is used to expand all elements of the array 'colors'.
  • The "echo" command prints the entire array, displaying each element separated by spaces.

10.

Using Special Variables:

Write a Bash script that utilizes special variables like $0, $#, $@, and $? in a script and display their values.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Print the name of the script
echo "Name of the script: $0"

# Print the number of arguments passed to the script
echo "Number of arguments passed: $#"

# Print all the arguments passed to the script
echo "All arguments passed: $@"

# Check the exit status of the last command
echo "Exit status of the last command: $?"

Output:

Name of the script: Main.sh
Number of arguments passed: 0
All arguments passed: 
Exit status of the last command: 0

Explanation:

In the exercise above,

  • $0 represents the name of the script itself.
  • $# represents the number of arguments passed to the script.
  • $@ represents all the arguments passed to the script.
  • $? represents the exit status of the last command executed in the script.

When we execute this script with arguments, it will display the name of the script, the number of arguments passed, all the arguments passed, and the exit status of the last command.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.