Recall & Review
beginner
What does
${#var} do in bash scripting?It returns the length (number of characters) of the string stored in the variable
var.Click to reveal answer
beginner
How do you find the length of the string stored in variable
name?Use
${#name} to get the number of characters in name.Click to reveal answer
beginner
What will be the output of this bash code?<br>
text="hello"
echo ${#text}The output will be
5 because the string "hello" has 5 characters.Click to reveal answer
beginner
Can
${#var} be used on empty strings? What is the result?Yes, it can. If the string is empty,
${#var} returns 0.Click to reveal answer
intermediate
Is
${#var} a command or a parameter expansion in bash?It is a parameter expansion that returns the length of the string stored in the variable
var.Click to reveal answer
What does
${#var} return in bash?✗ Incorrect
${#var} returns the number of characters in the string stored in var.If
var="abcde", what is the output of echo ${#var}?✗ Incorrect
The string "abcde" has 5 characters, so
${#var} outputs 5.What will
echo ${#empty} print if empty=""?✗ Incorrect
An empty string has length 0, so
${#empty} returns 0.Which of these is the correct way to get string length in bash?
✗ Incorrect
In bash,
${#var} is the correct syntax to get the length of a string.If
var="hello world", what does echo ${#var} output?✗ Incorrect
The string "hello world" has 11 characters including the space.
Explain how to find the length of a string stored in a variable in bash using ${#var}.
Think about how bash expands variables to get string length.
You got /3 concepts.
Describe what happens when you use ${#var} on a variable containing spaces or special characters.
Consider the string as a sequence of characters.
You got /3 concepts.