HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Strings

Strings

Length (${#name}), uppercase (${name^^}) and substring (${name:0:3}).

Simple enough for a 5-year-old to understand

Length and uppercase

Example code
1
name="hexlibrary"
2
echo ${#name}
Length: 10
Output / InputLength: 10
3
echo ${name^^}
Uppercase: HEXLIBRARY
Output / InputUppercase: HEXLIBRARY
Output / Input
💡 Hint: ${#name} length, ${name^^} uppercase, ${name,,} lowercase (curly braces are required).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the "hexlibrary" variable in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

In Bash, every value is fundamentally text, and at the heart of working with text is the use of QUOTES. Quotes in Bash are not decoration; they are a semantic tool: where they are used determines whether the script runs correctly.

There are three quote forms, and all three behave differently. Double quotes expand variables but prevent word splitting; single quotes expand nothing and take the content as-is; unquoted writing expands the variable AND splits the result into words.

This final behavior is the single largest source of bugs in Bash scripts. When you use a filename containing a space without quotes, the shell thinks it is two separate arguments. The rule is simple: always enclose variable expansion in double quotes.

What you will learn

  • Defining a text variable
  • The difference between double, single, and unquoted writing
  • The danger of leaving a variable unquoted
  • Getting text length and substrings
  • Concatenating text

When to use?

Variable expansion should always be enclosed in double quotes; leaving it unquoted is correct only in rare cases where you KNEW you wanted word splitting.

Single quotes are used for literal text where you do not want variable expansion; this is especially necessary for text containing dollar signs.

Common mistakes

Wrong

file="my document.txt"
rm $file

Unquoted expansion splits into words: the command receives two separate file names and tries to delete two non-existent files. The original file is not deleted, and two "No such file" errors are returned. Worse yet, in some commands this behavior leads to data loss.

Correct

file="my document.txt"
rm "$file"

Wrong

name="Ali"
echo 'Hello $name'

Single quotes perform no expansion whatsoever; a literal dollar sign and the variable name are printed to the screen. There is no error message, only incorrect output. If you want to see the variable's value, you must use double quotes.

Correct

name="Ali"
echo "Hello $name"

Wrong

prefix="log"
echo "${prefix}_file.txt"
echo "$prefix_file.txt"

On the second line, the shell reads the variable name as "prefix_file" because the underscore is a valid name character; since that variable is undefined, it prints as blank. There is no error message. Curly braces are needed to specify where the variable name ends.

Correct

prefix="log"
echo "${prefix}_file.txt"

Frequently asked questions

When can I leave a variable unquoted?

Almost never. The exception is when you deliberately want the expansion result to be split into multiple arguments — for example, a list of options stored in a variable. Other than that, leaving it unquoted is a mistake and linters will report it as a warning.

Which characters remain special inside double quotes?

The dollar sign, backslash, and command substitution syntax. Everything else is taken literally. Therefore, fixed texts containing a dollar sign — such as prices or regular expressions — should be written with single quotes.

How do I convert text to lowercase or uppercase?

Bash string expansion has special syntax for this, and since they don't invoke external programs, they are fast. In older scripts, the tr command is seen for the same task; it works, but it starts a new process on every call, making it slow inside a loop.

Summary

Variable expansion must always be enclosed in double quotes.

Single quotes perform no expansion, while double quotes expand variables.

Unquoted expansion splits into words and breaks values with spaces.

Curly braces are used to specify where the variable name ends.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code