Bash Trim: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:


=Overview=
=Overview=
==Trim Spaces==
With variable manipulation:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
trailing_space_removed=${var_name%%[[:space:]]}
trailing_space_removed=${var_name%%[[:space:]]}
leading_space_removed=${var_name##[[:space:]]}
leading_space_removed=${var_name##[[:space:]]}
</syntaxhighlight>
</syntaxhighlight>
With <code>sed</code>:
<syntaxhighlight lang='bash'>
sed -e 's/^ *//' -e 's/ *$//'
</syntaxhighlight>
==Trim New Line==
With <code>tr</code>: {{Internal|Tr#-d|<tt>tr -d</tt>}}

Latest revision as of 01:08, 2 March 2024

Internal

Overview

Trim Spaces

With variable manipulation:

trailing_space_removed=${var_name%%[[:space:]]}
leading_space_removed=${var_name##[[:space:]]}

With sed:

sed -e 's/^ *//' -e 's/ *$//'

Trim New Line

With tr:

tr -d