work with strings in bash
Join, cut, replace, change the case of and trim strings in a script, without other programs.
shelldevelopment
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.
ask for input in a script
Ask yes or no, read a password without showing it, or wait for an answer with a time limit.
check if a file or folder exists
Test in a script whether a path exists, is a file, a folder, empty or runnable.
check if a string contains a text
Test in a script whether a string contains, starts with or matches something.
find the script's own folder
Inside a script, find the folder the script is in, to reach files next to it.