Introduction
String manipulation is common because many tasks involve working with text data like filenames, user input, or command output. Changing or extracting parts of text helps automate and simplify these tasks.
var="Hello World" # Extract substring: ${var:6:5} # Replace substring: ${var/World/Bash} # Length of string: ${#var}
text="Hello World" echo ${text:6:5}
text="Hello World"
echo ${text/World/Bash}text="Hello World" echo ${#text}
#!/bin/bash input="file_2024_report.txt" # Extract year from filename year=${input:5:4} # Replace underscores with spaces newname=${input//_/ } echo "Year extracted: $year" echo "New filename: $newname"
name in bash?text="Hello World"
echo ${text:6:5}animal:animal="cat and cat"
echo ${animal/cat/dog}email="user@sub@example.com" using bash string manipulation. Which command correctly extracts example.com?Pass the test and this pattern moves unknown