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:14
I need to do this:
Code Sample
LIST=$LIST$ITEM[B]$LINEBREAK[/B]


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:18
There are a few ways to do it, depending on how you want to use the variable.

One way is:
Code Sample
LIST=`echo -e "$LIST$ITEM\n"`


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:49
I've tried that before. Doesn't wotk. :(

I tried this also:
Code Sample
BREAK=`echo -e "\n"`

Posted by clacker on Oct. 22 2006,21:07
You can try this, I just did and it works:

Code Sample
LIST="This and "
ITEM="That"
LINEBREAK="
"
TEST1=$LIST$ITEM[b]$LINEBREAK[/b]
TEST2="$LIST$ITEM[b]$LINEBREAK[/b]"

echo $TEST1
echo $TEST2


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:03
I had those quotes on my script.
But that
Code Sample
BREAK="
"
.. still didn't worked.

So it must be somewhere else...

Posted by Zucca on Oct. 22 2006,23:47
Solved. 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:
Code Sample
if [ -z "$PROGRAMPATH" ]
then
 echo "Program not found."
 exit 1
fi


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:58
Quotes 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:02
I 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... :p

Posted by mikshaw on Oct. 23 2006,15:41
One 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:
Code Sample
for i in $@; do echo $i;done
and you run scriptname 1 2 3 or scriptname "1 2 3", they both print 3 parameters.
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:06
Interesting, I was doing something with that quite recently as well.
man bash:
Code Sample
  Special Parameters
      The  shell  treats  several parameters specially.  These parameters may
      only be referenced; assignment to them is not allowed.
      *      Expands to the positional parameters, starting from  one.   When
             the  expansion occurs within double quotes, it expands to a sin-
             gle word with the value of each parameter separated by the first
             character of the IFS special variable.  That is, "$*" is equiva-
             lent to "$1c$2c...", where c is the first character of the value
             of  the IFS variable.  If IFS is unset, the parameters are sepa-
             rated by spaces.  If IFS is  null,  the  parameters  are  joined
             without intervening separators.
      @      Expands  to  the positional parameters, starting from one.  When
             the  expansion  occurs  within  double  quotes,  each  parameter
             expands to a separate word.  That is, "$@" is equivalent to "$1"
             "$2" ...  When there are no positional parameters, "$@"  and  $@
             expand to nothing (i.e., they are removed).
They're 'special'...

Posted by mikshaw on Oct. 23 2006,17:13
So 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....
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.