📜  bash echo in variable - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:39:27.553000             🧑  作者: Mango

Bash Echo in Variable - Shell-Bash

Bash is a popular shell used in most Linux-based operating systems. It is widely used for scripting and automation tasks.

One of the most common tasks in bash scripting is to store the output of a command or a piece of text in a variable and later display it using the echo command.

Store Output in a Variable

To store the output of a command in a variable, we use the following syntax:

variable=$(command)

For example, to store the output of the ls command in a variable, we can do the following:

files=$(ls)
echo $files

Here, we first store the output of the ls command in the files variable, and then display it using the echo command.

Store Text in a Variable

To store text in a variable, we can simply assign the text to a variable:

text="Hello World"
echo $text

Here, we store the text "Hello World" in the text variable, and then display it using the echo command.

Display Variable in Echo Command

To display a variable using the echo command, we simply specify the variable name with a $ symbol before it:

name="John"
echo "Hello $name"

Here, we display the text "Hello" followed by the value of the name variable using the echo command.

Conclusion

In summary, we can store the output of a command or text in a variable and later display it using the echo command in bash scripting. This is a fundamental concept in bash scripting and is used in most scripts.