LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-12-2009, 01:23 AM   #1
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Rep: Reputation: 15
How to do a "next line" inside a string?


Code:
string1=aaa
string2=bbb
newstring=$aaa$bbb
The new string is
Code:
aaabbb
How do i add a "next line" or a \r\n in between the 2 string so that the newstring, when echo, will be

Code:
aaa
bbb
 
Old 02-12-2009, 01:51 AM   #2
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Original Poster
Rep: Reputation: 15
hm..
i think it is not possible.
or is it not...
 
Old 02-12-2009, 01:55 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
From the echo man page:

Code:
NAME
       echo - display a line of text

SYNOPSIS
       echo [OPTION]... [STRING]...

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

       -E     disable interpretation of backslash escapes (default)

       --help display this help and exit

       --version
              output version information and exit

       If -e is in effect, the following sequences are recognized:

       \0NNN  the character whose ASCII code is NNN (octal)

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     suppress trailing newline

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab
Hope it helps.
 
Old 02-12-2009, 02:08 AM   #4
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Original Poster
Rep: Reputation: 15
Unhappy

i never once thought the critical link lies within echo....
 
Old 02-12-2009, 02:08 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Code:
newstring=$string1$'\n'$string2
Yves.
 
Old 02-12-2009, 02:22 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Quote:
How do i add a "next line" or a \r\n in between the 2 string so that the newstring, when echo, will be
Code:
aaa
bbb
Note that \r\n is the “new line” for DOS/Windows only. On Unix/Linux, the new line is \n only, and on Mac (at least up to OS9, I don't know for OS10+) the new line is \r only.
If you want to force a DOS new line, then in my previous post, replace $'\n' with $'\r\n'.

Another thing, don't forget that bash and echo have a very HTML-like behaviour with blanks when quotes are omited*: any run of blanks, be it spaces or carriage returns or line feeds… or any mix, is replaced with a single character (the first character of the IFS environment variable, which usually is a space).

Example:
Code:
[yves@localhost ~]$ test1='this is
> test #1'
[yves@localhost ~]$ test2='that      is
test      #2'
[yves@localhost ~]$ echo $test1 $test2
this is test #1 that is test #2
[yves@localhost ~]$ echo "$test1" "$test2"
this is
test #1 that      is
test      #2
[yves@localhost ~]$ echo "$test1"$'\n'"$test2"
this is
test #1
that      is
test      #2
[yves@localhost ~]$ echo $test1$'\n'$test2
this is test #1
that is test #2
Yves.
 
Old 02-12-2009, 02:29 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by theYinYeti View Post
Another thing, don't forget that bash and echo have a very HTML-like behaviour with blanks when quotes are omited*: any run of blanks, be it spaces or carriage returns or line feeds… or any mix, is replaced with a single character (the first character of the IFS environment variable, which usually is a space).
Yves.
Yep. That reminds me of another way to achieve the same effect:

Code:
$ IFS="\n"; echo "1
> 2
> 3
> "
1
2
3
 
Old 02-12-2009, 02:37 AM   #8
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Original Poster
Rep: Reputation: 15


That's very interesting.
Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
if [ "$VAR"="string" ] always testing TRUE JohnE1 Programming 2 08-05-2008 08:35 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
How to convert "string" type to "char * " in c++ pranavojha Programming 18 06-05-2008 03:10 PM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM
Symlinks working from "inside" 'chroot' to "outside" ? amn Linux - Newbie 2 12-07-2007 01:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration