0
0
Bash Scriptingscripting~5 mins

String length (${#var}) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AValue of the variable var
BFirst character of var
CLength of the string in variable var
DNumber of words in var
If var="abcde", what is the output of echo ${#var}?
A5
B4
Cabcde
DError
What will echo ${#empty} print if empty=""?
Aempty
B0
C1
DError
Which of these is the correct way to get string length in bash?
A${#var}
Blen(var)
Clength(var)
Dvar.length
If var="hello world", what does echo ${#var} output?
AError
B10
C2
D11
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.