Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove line breaks in Bourne Shell from variable

In bourne shell I have the following:

VALUES=`some command that returns multiple line values`

echo $VALUES

Looks like:

"ONE"
"TWO"
"THREE"
"FOUR"

I would like it to look like:

"ONE" "TWO" "THREE" "FOUR"

Can anyone help?

like image 538
Chris Kannon Avatar asked Jan 20 '10 16:01

Chris Kannon


People also ask

How do I remove a new line character from a variable in Unix?

If you are using bash , you can use Parameter Expansion: dt=${dt//$'\n'/} # Remove all newlines. dt=${dt%$'\n'} # Remove a trailing newline.


2 Answers

echo $VALUES | tr '\n' ' '

like image 197
eliah Avatar answered Sep 20 '22 17:09

eliah


Another method, if you want to not just print out your code but assign it to a variable, and not have a spurious space at the end:

$ var=$(tail -1 /etc/passwd; tail -1 /etc/passwd)
$ echo "$var"
apache:x:48:48:Apache:/var/www:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
$ var=$(echo $var)
$ echo "$var"     
apache:x:48:48:Apache:/var/www:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin
like image 23
user3438686 Avatar answered Sep 21 '22 17:09

user3438686