- How can I have a newline in a string in sh?
- 13 Answers 13
- How to output a multiline string in Bash?
- 9 Answers 9
- Echo newline in Bash prints literal \n
- 23 Answers 23
- How to concatenate multiple lines of output to one line?
- 11 Answers 11
- The fastest and easiest ways I know to solve this problem:
- Convert an output to string
- 5 Answers 5
- How to fix the problem
- Why you got the error message
How can I have a newline in a string in sh?
produces as output
What should I do to have a newline in a string?
Note: This question is not about echo. I’m aware of echo -e , but I’m looking for a solution that allows passing a string (which includes a newline) as an argument to other commands that do not have a similar option to interpret \n ‘s as newlines.
13 Answers 13
If you’re using Bash, the solution is to use $’string’ , for example:
If you’re using pretty much any other shell, just insert the newline as-is in the string:
Bash is pretty nice. It accepts more than just \n in the $» string. Here is an excerpt from the Bash manual page:
Echo is so nineties and so fraught with perils that its use should result in core dumps no less than 4GB. Seriously, echo’s problems were the reason why the Unix Standardization process finally invented the printf utility, doing away with all the problems.
So to get a newline in a string, there are two ways:
There! No SYSV vs BSD echo madness, everything gets neatly printed and fully portable support for C escape sequences. Everybody please use printf now for all your output needs and never look back.
What I did based on the other answers was
I find the -e flag elegant and straight forward
If the string is the output of another command, I just use quotes
The problem isn’t with the shell. The problem is actually with the echo command itself, and the lack of double quotes around the variable interpolation. You can try using echo -e but that isn’t supported on all platforms, and one of the reasons printf is now recommended for portability.
You can also try and insert the newline directly into your shell script (if a script is what you’re writing) so it looks like.
The only simple alternative is to actually type a new line in the variable:
Yes, that means writing Enter where needed in the code.
There are several equivalents to a new line character.
But all those require «an interpretation» by some tool (POSIX printf):
And therefore, the tool is required to build a string with a new-line:
In some shells, the sequence $ ‘ is an special shell expansion. Known to work in ksh93, bash and zsh:
Of course, more complex solutions are also possible:
A $ right before single quotation marks ‘. \n. ‘ as follows, however double quotation marks doesn’t work.
I’m no bash expert, but this one worked for me:
I found this easier to formatting the texts.
Disclaimer: I first wrote this and then stumbled upon this question. I thought this solution wasn’t yet posted, and saw that tlwhitec did post a similar answer. Still I’m posting this because I hope it’s a useful and thorough explanation.
Short answer:
This seems quite a portable solution, as it works on quite some shells (see comment).
This way you can get a real newline into a variable.
The benefit of this solution is that you don’t have to use newlines in your source code, so you can indent your code any way you want, and the solution still works. This makes it robust. It’s also portable.
Longer answer:
Explanation of the above solution:
The newline would normally lost due to command substitution, but to prevent that, we add a ‘q’ and remove it afterwards. (The reason for the double quotes is explained further below.)
We can prove that the variable contains an actual newline character (0x0A):
(Note that the ‘%s’ was needed, otherwise printf will translate a literal ‘\n’ string into an actual 0x0A character, meaning we would prove nothing.)
Of course, instead of the solution proposed in this answer, one could use this as well (but. ):
. but that’s less robust and can be easily damaged by accidentally indenting the code, or by forgetting to dedent it afterwards, which makes it inconvenient to use in (indented) functions, whereas the earlier solution is robust.
Now, as for the double quotes:
The reason for the double quotes » surrounding the command substitution as in nl=»$(printf ‘\nq’)» is that you can then even prefix the variable assignment with the local keyword or builtin (such as in functions), and it will still work on all shells, whereas otherwise the dash shell would have trouble, in the sense that dash would otherwise lose the ‘q’ and you’d end up with an empty ‘nl’ variable (again, due to command substitution).
That issue is better illustrated with another example:
Источник
How to output a multiline string in Bash?
How can I output a multipline string in Bash without using multiple echo calls like so:
I’m looking for a portable way to do this, using only Bash builtins.
9 Answers 9
Here documents are often used for this purpose.
They are supported in all Bourne-derived shells including all versions of Bash.
or you can do this:
Inspired by the insightful answers on this page, I created a mixed approach, which I consider the simplest and more flexible one. What do you think?
First, I define the usage in a variable, which allows me to reuse it in different contexts. The format is very simple, almost WYSIWYG, without the need to add any control characters. This seems reasonably portable to me (I ran it on MacOS and Ubuntu)
Then I can simply use it as
or even better, when parsing parameters, I can just echo it there in a one-liner:
Use -e option, then you can print new line character with \n in the string.
Sample (but not sure whether a good one or not)
The fun thing is that -e option is not documented in MacOS’s man page while still usable. It is documented in the man page of Linux.
Since I recommended printf in a comment, I should probably give some examples of its usage (although for printing a usage message, I’d be more likely to use Dennis’ or Chris’ answers). printf is a bit more complex to use than echo . Its first argument is a format string, in which escapes (like \n ) are always interpreted; it can also contain format directives starting with % , which control where and how any additional arguments are included in it. Here are two different approaches to using it for a usage message:
First, you could include the entire message in the format string:
Note that unlike echo , you must include the final newline explicitly. Also, if the message happens to contain any % characters, they would have to be written as %% . If you wanted to include the bugreport and homepage addresses, they can be added quite naturally:
Second, you could just use the format string to make it print each additional argument on a separate line:
With this option, adding the bugreport and homepage addresses is fairly obvious:
Источник
Echo newline in Bash prints literal \n
In Bash, tried this:
But it doesn’t print a newline, only \n . How can I make it print the newline?
I’m using Ubuntu 11.04 (Natty Narwhal).
23 Answers 23
You could use printf instead:
printf has more consistent behavior than echo . The behavior of echo varies greatly between different versions.
Make sure you are in Bash. All these four ways work for me:
Words of the form $’string‘ are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
You could always do echo «» .
It worked for me in the nano editor.
From the man page:
-e enable interpretation of backslash escapes
In the off chance that someone finds themselves beating their head against the wall trying to figure out why a coworker’s script won’t print newlines, look out for this:
As in the above, the actual running of the method may itself be wrapped in an echo which supersedes any echos that may be in the method itself. Obviously I watered this down for brevity. It was not so easy to spot!
You can then inform your comrades that a better way to execute functions would be like so:
Источник
How to concatenate multiple lines of output to one line?
If I run the command cat file | grep pattern , I get many lines of output. How do you concatenate all lines into one line, effectively replacing each «\n» with «\» » (end with » followed by space)?
cat file | grep pattern | xargs sed s/\n/ /g isn’t working for me.
11 Answers 11
Use tr ‘\n’ ‘ ‘ to translate all newline characters to spaces:
Note: grep reads files, cat concatenates files. Don’t cat file | grep !
Edit:
tr can only handle single character translations. You could use awk to change the output record separator like:
This would transform:
Piping output to xargs will concatenate each line of output to a single line with spaces:
Or any command, eg. ls | xargs . The default limit of xargs output is
4096 characters, but can be increased with eg. xargs -s 8192 .
In bash echo without quotes remove carriage returns, tabs and multiple spaces
This could be what you want
As to your edit, I’m not sure what it means, perhaps this?
(this assumes that
does not occur in file )
This is an example which produces output separate by commas. You can replace the comma by whatever separator you need.
Here is the method using ex editor (part of Vim):
Join all lines and print to the standard output:
Join all lines in-place (in the file):
Note: This will concatenate all lines inside the file it-self!
The fastest and easiest ways I know to solve this problem:
When we want to replace the new line character \n with the space:
xargs has own limits on the number of characters per line and the number of all characters combined, but we can increase them. Details can be found by running this command: xargs —show-limits and of course in the manual: man xargs
When we want to replace one character with another exactly one character:
When we want to replace one character with many characters:
First, we replace the newline characters \n for tildes
(or choose another unique character not present in the text), and then we replace the tilde characters with any other characters ( many_characters ) and we do it for each tilde (flag g ).
Источник
Convert an output to string
I’m trying do to a script to check the CA power status
this is my code:
It’s not working; the variable $a ia not compare with the the string «AC adapter : online». How to convert the output of command acpitool -a to a string?
This is what happens:
Problem solved!
This is the new code, whith the help of all of you, thanks.
This code may be use on a server, to alert if the power fails!
5 Answers 5
How to fix the problem
The shell (or the test command) uses = for string equality and -eq for numeric equality. Some versions of the shell support == as a synonym for = (but = is defined by the POSIX test command). By contrast, Perl uses == for numeric equality and eq for string equality.
You also need to use one of the test commands:
With the [[ operator, you could drop the quotes around «$a» .
Why you got the error message
the shell expanded it to:
which is a request to execute the command AC with the 5 arguments shown, and compare the exit status of the command with 0 (considering 0 — success — as true and anything non-zero as false). Clearly, you don’t have a command called AC on your system (which is not very surprising).
This means you can write:
If you want to test strings, you have to use the test command or the [[ . ]] operator. The test command is the same as the [ command except that when the command name is [ , the last argument must be ] .
Источник