Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2183  / 2 Years ago, sun, september 11, 2022, 11:22:26

In a shell script I tried to replace blank space by
with this command



echo -e $var |tr ' ' '
'


Outside the script it workd perfectly but inside there is no effect .
This how i use in the script:



var =$(echo -e $var | tr ' ' '
' )


Thanks .


More From » bash

 Answers
3

After expanding a parameter expansion ($var) or command substitution ($(cmd)), the shell removes all whitespace from the expanded result in order to split them into words (word splitting). On top of that, it tries to match filenames for the words that contain *, ? and/or [...] (pathname expansion). So always enclose expansions in double quotes ("$var" and "$(cmd)") to avoid word splitting and pathname expansion to be attempted.



$ var=$'two
lines here'
$ echo $var
two lines here
$ echo "$var"
two
lines here
$


Hence:



echo "$var" | tr ' ' '
'
# or using bash's more powerful types of parameter expansions
echo "${var// /$'
'}"

var="$(echo "$var" | tr ' ' '
')"


See also: http://mywiki.wooledge.org/Quotes


[#36294] Monday, September 12, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
piscen

Total Points: 134
Total Questions: 117
Total Answers: 133

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;