Shell Script: how to trim spaces from a bash variable

Possible duplicate:
How to trim whitespace from a bash variable?

I searched and tried to make some decisions, but nothing works for me ...

I have a shell variable that causes problems due to start and end spaces. how can we get rid of all spaces in one line using shell script?

+6
source share
2 answers

I can imagine two options:

variable=" gfgergj lkjgrg " echo $variable | sed 's,^ *,,; s, *$,,' 

or more

 nospaces=${variable## } # remove leading spaces nospaces=${variable%% } # remove trailing spaces 
+13
source

There are so many ways to achieve this, awk oneliner:

 kent$ echo " foo - - - bar "|awk '{sub(/^ */,"",$0);sub(/ *$/,"",$0)}1' foo - - - bar 
+1
source

Source: https://habr.com/ru/post/922018/


All Articles