In this article we will provide you bash script code to remove all the extra white spaces in the string at any location. This will sanitize the provided string completely. There won’t be extra spaces at start, end or in between characters or words.
Code Example –
#!/bin/bash # shellcheck disable=SC2086,SC2048 trim_all() { set -f set -- $* echo "$*" set +f } sanitized_string=$(trim_all " There is enough vegetables. You are not going to die with hunger if you stop eating animals . ") echo $sanitized_string # output: There is enough vegetables. You are not going to die with hunger if you stop eating animals .