work with strings in bash
Join, cut, replace, change the case of and trim strings in a script, without other programs.
Steps
-
full="$first $last"join strings
-
text+=' more'add to the end
-
echo "${text:0:5}"the first 5 characters
-
echo "${text//old/new}"replace every old with new
-
echo "${text,,} ${text^^}"lower case and upper case (bash 4)
-
echo "${#text}"its length
-
trimmed=$(echo "$text" | xargs)without spaces at the ends
Commands used
xargsprintf
Each has a page in lore: lore xargs opens it.
Related recipes
- ask for input in a scriptAsk yes or no, read a password without showing it, or wait for an answer with a time limit.
- check if a file or folder existsTest in a script whether a path exists, is a file, a folder, empty or runnable.
- check if a string contains a textTest in a script whether a string contains, starts with or matches something.
- find the script's own folderInside a script, find the folder the script is in, to reach files next to it.