Prepare for the CompTIA PenTest+ Exam. Study with flashcards and multiple choice questions; each comes with hints and explanations. Get ready for your certification!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


In Bash, how are variables assigned?

  1. my_str = "Hello, World!"

  2. my_str:= "Hello, World!"

  3. my_str === "Hello, World!"

  4. my_str="Hello, World!"

The correct answer is: my_str="Hello, World!"

In Bash, variables are assigned using the syntax where you specify the variable name followed immediately by an equal sign, without spaces, and then the value you want to assign. The correct format is `my_str="Hello, World!"`, which adheres to the rules of shell scripting that require no whitespace around the equal sign. This method makes the variable `my_str` hold the string "Hello, World!", which can then be utilized later in the script. The other choices provide incorrect formats for variable assignment in Bash. Using spaces around the equal sign, as in the first choice, is not allowed and will result in an error. The second choice employs the wrong operator (`:=`), which is not valid in Bash for simple variable assignments. The third choice utilizes a triple equals sign (`===`), which is relevant in other programming languages but does not apply to variable assignments in Bash.