|
Home > Archive > Unix Shell > February 2007 > Concatenate a string that spans multiple lines in bsh
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Concatenate a string that spans multiple lines in bsh
|
|
| lovecreatesbea...@gmail.com 2007-02-16, 1:17 pm |
| I have a string variable contains long content, for example more than
80 letters. How can I concatenate the lines of content? I've tried
ways like the following, but failed. I put a tab in the front of
"world" in source file for indent, but I don't want the result string
to contain a tab character.
s="hello ... \
world ..."
| |
| Janis Papanagnou 2007-02-16, 1:17 pm |
| lovecreatesbea...@gmail.com wrote:
> I have a string variable contains long content,
You mean a string literal (a constant string value)?
> for example more than
> 80 letters. How can I concatenate the lines of content?
If it's a constant literal why not just type it all until its end?
(If your editor inserts hard linebreaks switch off that feature or
use a real editor.)
> I've tried
> ways like the following, but failed. I put a tab in the front of
> "world" in source file for indent, but I don't want the result string
> to contain a tab character.
If you want to split a string literal into several literals and
assign each one separately...
s=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN
OPQRSTUVWXYZ"
s=$s" 0123456789012345678901234567890123456789
0123456789"
s=$s"whatever..."
Janis
>
> s="hello ... \
> world ..."
>
| |
| kruhft 2007-02-16, 1:17 pm |
| lovecreatesbea...@gmail.com wrote:
> I have a string variable contains long content, for example more than
> 80 letters. How can I concatenate the lines of content? I've tried
> ways like the following, but failed. I put a tab in the front of
> "world" in source file for indent, but I don't want the result string
> to contain a tab character.
>
> s="hello ... \
> world ..."
First off you don't need the line continuation characters in double
quotes; the double quoting mechanism handles that for you. If you
don't want the newlines in the output, you can do something like this:
$ x="a # note that $ and > are primary and secondary prompts
> b
> c"
$ echo ${x//\n/} # this will remove the \n characters from the
variable
a b c
I didn't put tabs in the above example, but you can remove them using
a similar expansion by assigning x to another variable using the
substitution expansion above and then doing it again:
$ y=${x//\n/} # get rid of newlines
$ echo ${y//\t/} # get rid of tabs
or you could do something fancier using sed:
$ echo $x | sed -e 's|\n| |;s|^\t||' # subst newlines with a space
and remove tabs at the start of the line
--
kruhft
| |
| Chris F.A. Johnson 2007-02-17, 7:15 pm |
| On 2007-02-16, lovecreatesbea...@gmail.com wrote:
> I have a string variable contains long content, for example more than
> 80 letters. How can I concatenate the lines of content? I've tried
> ways like the following, but failed. I put a tab in the front of
> "world" in source file for indent, but I don't want the result string
> to contain a tab character.
If you don't want a tab, don't enter one.
> s="hello ... \
> world ..."
What *do* you want $s to contain?
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
|
|
|