How to assing a line break to a variable in bash?
Forum: Programming and Scripting
Topic: How to assing a line break to a variable in bash?
started by: Zucca
Posted by Zucca on Oct. 22 2006,16:14I need to do this:
I've tried even to use 'echo' to add line break but none has worked.
I've also searched Google already...
Posted by mikshaw on Oct. 22 2006,17:18There are a few ways to do it, depending on how you want to use the variable.
One way is:
Another way is to use a different type of separator, such as a space or comma, when setting the variable, and then using tr to replace the separator with a newline when the variable is called.
Posted by Zucca on Oct. 22 2006,19:49I've tried that before. Doesn't wotk.
I tried this also:
Posted by clacker on Oct. 22 2006,21:07You can try this, I just did and it works:
The second test succeeds because of the quotes, the first one fails by putting everything on one line. You need the quotes when you assign the variable.
Posted by Zucca on Oct. 22 2006,23:03I had those quotes on my script.
So it must be somewhere else...
Posted by Zucca on Oct. 22 2006,23:47Solved. There was missing one double quote AND I didn't knew if I need to check variable if it contains a path it has to be done like this:
Double quotes around variable name. ;)
Then it looks that variable as text.
Need to read more bash manuals. ;)
Posted by mikshaw on Oct. 23 2006,03:58Quotes on variables can be a frustrating thing...I still don't fully understand all the differences between using them in some cases and not in others. Sometimes it works as desired with or without quotes, sometimes it works either way but with different results depending on whether or not quotes are used. Other times you definitely need quotes.
Posted by ^thehatsrule^ on Oct. 23 2006,15:02I try to use quotes when you mean to parse the variables as strings.
btw, please please double check your topic titles - they can be misleading...
Posted by mikshaw on Oct. 23 2006,15:41One thing I've only recently noticed, and still attempting to get a solid understanding of it, is how commandline parameters are affected by quotes.
If you have this script:
If the $@ in the script is enclosed in quotes ("$@"), you notice a difference. Sending "1 2 3" in quotes as a single string is interpreted as $1 (a single parameter), but sending 1 2 3 without quotes is interpreted as 3 separate parameters.
Posted by ^thehatsrule^ on Oct. 23 2006,16:06Interesting, I was doing something with that quite recently as well.
Posted by mikshaw on Oct. 23 2006,17:13So it's done by design. I was thinking that it works as I would expect when @ is in quotes, but I'm still not sure of the purpose of having the difference, unless there is sometimes a need to obliterate the use of spaces within individual parameters. I guess the point is mainly just to have that option....