Programming and Scripting :: How to assing a line break to a variable in bash?



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. ;)

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.
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

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.

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'...

Next Page...
original here.