How to combine text and variables?

071

You can combine text and variables in a few different ways using the echo command. Here are some examples:

1. Using echo with Variables

You can directly include the variable within double quotes:

name="Alice"
echo "My name is $name"

This will output:

My name is Alice

2. Using Commas

You can also separate text and variables with commas:

age=30
echo "I am", $age, "years old."

This will output:

I am 30 years old.

3. Concatenating Strings

You can concatenate strings and variables without spaces:

greeting="Hello"
name="Bob"
echo "$greeting, $name!"

This will output:

Hello, Bob!

4. Using Curly Braces

For clarity, especially when using variables next to text, you can use curly braces:

name="Charlie"
echo "Welcome, ${name}!"

This will output:

Welcome, Charlie!

Using these methods, you can effectively combine text and variables in your shell scripts or terminal commands.

0 Comments

no data
Be the first to share your comment!