This script demonstrates basic string manipulation in a Linux shell environment, specifically within WSL (Windows Subsystem for Linux) on Windows 11. It focuses on trimming whitespace and special characters, and changing the case of strings.
#!/bin/bash
# remove "\n" "-" " "
phone=" 04-1111-2222 "
echo "$phone" | tr -d " -" | tr -d '\n'
trim=$(echo "$phone" | tr -d "-")
echo "$trim" | tr -d '\n'
# lowercase uppercase -------------------
fruit="Apple"
echo "$fruit" | tr A-Z a-z | tr -d '\n'
echo "$fruit" | tr a-z A-Z
# sed stream edit ---------------
echo "$phone" | sed 's/-//g' | tr -d '\n'
echo "$phone"
phone=" 04-1111-2222 ": Defines a string variable with leading/trailing spaces and hyphens.echo "$phone" | tr -d " -" | tr -d '\n': Uses tr to delete spaces, hyphens, and newline characters.trim=$(echo "$phone" | tr -d "-"): Stores the result of removing hyphens in the trim variable.echo "$trim" | tr -d '\n': Removes newline characters from the trim variable.fruit="Apple": Defines a string variable.echo "$fruit" | tr A-Z a-z | tr -d '\n': Converts uppercase to lowercase and removes newline.echo "$fruit" | tr a-z A-Z: Converts lowercase to uppercase.sed:
echo "$phone" | sed 's/-//g' | tr -d '\n': Uses sed to replace all hyphens with nothing (effectively deleting them) and then removes newline characters. s/-//g means substitute all (-) with nothing globally (g).echo "$phone": Prints the original phone variable.cd ./WECM100nano b009.sh and paste the script content.chmod +x b009.sh./b009.sh
0411112222
0411112222
apple
APPLE
0411112222
04-1111-2222
This script is a simple example for embedded beginners to understand basic string manipulation in a Linux shell environment. The tr and sed commands are powerful tools for text processing.