wiki/help/linux/bash/data_manipulation/string.md

24 lines
795 B
Markdown
Raw Normal View History

# string operations.
## case operations.
```bash
local myword="Hello"
echo ${myword^} # first letter uppercase.
echo ${myword^^} # all uppercase.
echo ${myword,} # first letter lowercase.
echo ${myword,,} # all lowercase.
echo ${myword~} # reverses the case for the first letter.
echo ${myword~~} # reverses the case for every letter.
```
## substitution.
```bash
local example="file.tar.gz"
echo "${example#file}" # ".tar.gz" - remove "file" from the start.
echo "${example%gz}" # "file.tar." - remove "gz" from the end.
echo "${example#*.}" # "tar.gz" - shortest from the start to dot.
echo "${example##*.}" # "gz" - longest from the start to dot.
echo "${example%.*}" # "file.tar" - shortest from the end to dot.
echo "${example%%.*}" # "file" - longest from the end to dot.
```