# string manipulation in bash shell

**URL:** <https://community.unix.com/t/string-manipulation-in-bash-shell/294919>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [September 8, 2011, 5:38am UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919 "2011-09-08T05:38:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pkumar\_Sachin](https://community.unix.com/letter_avatar/pkumar_sachin/32/5_5575768a8748004e209b776fc1b2916d.png) [@Pkumar\_Sachin](https://community.unix.com/u/Pkumar_Sachin)\
**Post date:** [September 8, 2011, 5:38am UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919/1 "2011-09-08T05:38:43Z")

</div>

Hi All,

I am using a bash shell and want to the following thing.

A process sends the following string to my script

BACKUP\_FAIL\_REASON="Failed - Application Dump CDMACA-0:grep: /opt/nortel/ca/data/1245184/sd00/image1/S110907070708HIS  
/opt/nortel/ca/data/1376256/sd00/image1/S110906070712HIS  
/opt/nortel/ca/data/1376256/sd00/images/S110906010711HIS  
/opt/nortel/ca/data/1376256/sd00/image1/S110906070712HIS  
/opt/nortel/ca/data/1376256/sd01/image1/S110907010713HIS  
/opt/nortel/ca/data/1376256/sd01/images/S110907070713HIS"

As you can see the string that i have has multiple newlines in it.

Please suggest me a mechanism to manipulate the string so as to remove the newlines from the string.

After the manipulation my string should contain no newlines and should have all the strings separated by spaces only.

Thanks,  
Sachin

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [September 8, 2011, 5:43am UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919/2 "2011-09-08T05:43:10Z")

</div>

[Google Search Results for remove newline | The UNIX and Linux Forums](http://www.unix.com/cfmgoogle.php?cx=partner-pub-6323928554267084%3Absye1r5tx7j&cof=FORID%3A10&q=remove+newline#947)

---

<div class="post-metadata">

**Author:** ![jayan\_jay](https://community.unix.com/user_avatar/community.unix.com/jayan_jay/32/1006_2.png) [@jayan\_jay](https://community.unix.com/u/jayan_jay)\
**Post date:** [September 8, 2011, 5:46am UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919/3 "2011-09-08T05:46:10Z")

</div>

```nohighlight
$ echo "$BACKUP_FAIL_REASON" | xargs

```

---

<div class="post-metadata">

**Author:** ![Pkumar\_Sachin](https://community.unix.com/letter_avatar/pkumar_sachin/32/5_5575768a8748004e209b776fc1b2916d.png) [@Pkumar\_Sachin](https://community.unix.com/u/Pkumar_Sachin)\
**Post date:** [September 12, 2011, 3:24am UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919/4 "2011-09-12T03:24:08Z")

</div>

Thanks for ur replys buddy :)🙂

I adopted the following to produce the desired result

BACKUP\_FAIL\_REASON = `echo -e "${BACKUP\_FAIL\_REASON}" | tr "\n" " "

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 12, 2011, 12:57pm UTC](https://community.unix.com/t/string-manipulation-in-bash-shell/294919/5 "2011-09-12T12:57:23Z")

</div>

If you remove the quotes from around "${BACKUP\_FAIL\_REASON}" you won't need the tr, echo will flatten it by itself.
