-
prompting the user to enter their name.
-
read name
: This command reads user input from the terminal and stores it in the variable named 'name'.
-
if [ -n "$name" ]; then
: This is a conditional statement that checks if the length of the variable 'name' is non-zero, meaning the user entered something.
-
Inside the if
block: If a name is provided (-n
check is true), it prints a personalized greeting using the entered name. If no name is provided, it prints a generic greeting.
-
else
: This part is executed if the condition in the if
statement is false.
-
fi
: This marks the end of the if
statement.
Save the script in a file, for example, greeting_script.sh
. To execute the script, make it executable with the command chmod +x greeting_script.sh
and then run it with ./greeting_script.sh
#!/bin/bash
# This is a simple Bash script that prompts the user for their name and prints a greeting.
# Prompt the user for their name
echo "Hello! Please enter your name:"
# Read user input into a variable named 'name'
read name
# Check if the user provided a name
if [ -n "$name" ]; then
# If a name is provided, print a greeting
echo "Hello, $name! Nice to meet you."
else
# If no name is provided, print a generic greeting
echo "Hello, anonymous! Nice to meet you."
fi
adding ts
#!/bin/bash
# This is a simple Bash script that prompts the user for their name and prints a greeting.
# Prompt the user for their name
echo "Hello! Please enter your name:"
# Read user input into a variable named 'name'
read name
# Check if the user provided a name
if [ -n "$name" ]; then
# If a name is provided, print a greeting
echo "Hello, $name! Nice to meet you."
else
# If no name is provided, print a generic greeting
echo "Hello, anonymous! Nice to meet you."
fi