Here are the steps on how to use variables in bash shell script to store and display your name, age, and city.
1. Create a new file as mentioned below.
vi script_name.sh
2. Copy and paste below simple Bash script that checks if a file exists in a specific directory:
#!/bin/bash #Variables name="your name" age=23 city=”your city” #Dispalying the values echo “Name: $name” echo “Age: $age” echo “City: $city”
Here’s a breakdown of the script:
#!/bin/bash : This line specifies the Bash interpreter.
name=”your name” : Assigning the user’s name into the variable “name”.
age=”your_age” : Assigning the user’s age into the variable “age”.
city=”your city” : Assigning the user’s city into the variable “city”.
echo “Name: $name”: Displaying the user’s name from the variable “name”.
echo “Age: $age” : Displaying the user’s age from the variable “age”.
echo “City: $city” : Displaying the user’s city from the variable “city”.
In this script, replace “Your Name”, 30, and “Your City” with your actual name, age, and city. When you run this script, it will display the values stored in the variables. Remember to make the script executable using the chmod +x scriptname.sh command before running it in your terminal.
3. Save and close the “vi” editor by pressing “Esc” key and type :wq!
4. Make sure to give execute permissions to the script before running it. You can do this by running the following command in the terminal:
chmod +x script_name.sh
Note : Replace “script_name.sh” with the actual name you save the script as.
Click Here!!! to know how to edit files using VIM editor in linux.
How do you feel about this post? Drop your comments below..