======================================== Bash String Manipulation Cheat Sheet ======================================== Author: Karandeep Singh Date: February 20, 2023 License: MIT License --------------------------------------------------------------- Function | Description | Usage Example --------------------------------------------------------------- ltrim | Removes leading whitespace | clean_input=$(ltrim " Hello World") rtrim | Removes trailing whitespace | clean_input=$(rtrim "Hello World ") trim | Removes both leading and trailing | clean_input=$(trim " Hello World ") | whitespace from a string | reverse | Reverses the characters in a string | reversed=$(reverse "Hello World") len | Returns the length of a string | length=$(len "Hello World") uppercase | Converts a string to uppercase | upper=$(uppercase "hello world") lowercase | Converts a string to lowercase | lower=$(lowercase "HELLO WORLD") substitute | Replaces a substring with another | result=$(substitute "Hello World" "World" "Bash") truncate | Truncates a string and appends | truncated=$(truncate "Hello World" 5) | an ellipsis | count | Counts occurrences of a substring | occurrences=$(count "Hello World, Hello Bash" "Hello") split | Splits a string into an array based | result=($(split "apple,banana,cherry" ",")) | on a delimiter | capitalize | Capitalizes the first letter of each | capitalized=$(capitalize "hello world from bash") | word in a string | rot13 | Encodes a string using the ROT13 | rot13_encoded=$(rot13 "Hello World") | cipher | index | Finds the position of the first | position=$(index "Hello World" "W") | occurrence of a substring | join | Joins an array of strings into a | joined=$(join "," "apple" "banana" "cherry") | single string with a delimiter | substring | Extracts a substring based on start | substr=$(substring "Hello World" 6 5) | index and length | repeat | Repeats a string a specified number | repeated=$(repeat "Hello " 3) | of times | lastindex | Finds the position of the last | last_pos=$(lastindex "Hello World, Hello Bash" "Hello") | occurrence of a substring | shuffle | Randomly shuffles the characters | shuffled=$(shuffle "Hello") | in a string | random_string | Generates a random string of a | rand_str=$(random_string 10) | specified length | sanitize | Removes non-alphanumeric characters, | sanitized=$(sanitize "Hello, @World! #2024" " ") | except those specified | remove_special_chars| Removes all special characters from | clean_string=$(remove_special_chars "Hello, @world!") | a string | reverse_words | Reverses the order of words in a | reversed_sentence=$(reverse_words "This is a sentence.") | string | strip_html_tags | Removes HTML tags from a given string | cleaned=$(strip_html_tags "

This is bold text.

") camel_to_snake_case | Converts CamelCase to snake_case | snake_case_str=$(camel_to_snake_case "camelCaseString") count_occurrences | Counts occurrences of a substring | count=$(count_occurrences "apple" "I like apple and apple pie.") transform_text | Transforms text by removing special | transformed_result=$(transform_text "Hello, world! This is an example.") | characters, converting to title case,| | and replacing words with synonyms | | using an external API | --------------------------------------------------------------- ======================================== License ======================================== MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.