Command Substitution in Shell

Level 261 keystrokes

$() captures command output and assigns it to a variable.

Shell code

# $(...) runs date and stores its output in today
# +%Y-%m-%d formats the date as YYYY-MM-DD, e.g. 2024-01-15
1today=$(date +%Y-%m-%d)
# ${today} is the same as $today; the braces mark where the name ends
# Prints: Backup: backup_2024-01-15.tar.gz (the date varies)
2echo "Backup: backup_${today}.tar.gz"

Output

Backup: backup_2024-01-15.tar.gz
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • %× 3
  • -× 2
  • .× 2
  • "× 2
  • $× 2
  • _× 1
  • :× 1
  • (× 1
  • )× 1
  • {× 1

More Shell examples at level 2

← Shell typing practice