How do you assign string values to variables for lines of code?
It's an issue
- I need to give a variable a value that is well-long
- The lines in my script must be under a specified number of columns
So i am trying to assign it to more than one line
It's simple to do without indents
VAR="This displays without \
any issues."
echo "${VAR}"
Result.
This displays without any issues.
However with indents.
VAR="This displays with \
extra spaces."
echo "${VAR}"
Result.
This displays with extra spaces.
How can i define a space for something without using spaces?
The problem here is that you surround the variable with double quotes Stop it and things will work
VAR="This displays with \
extra spaces."
echo ${VAR}
Output
This displays with extra spaces.
The problem is that double-quoting a variable preserves all white space characters This can be used in cases where you need it explicitly
For example,
$ echo "Hello World ........ ... ...."
will print
Hello World ........ ... ....
And on removing quotes, its different
$ echo Hello World ........ ... ....
Hello World ........ ... ....
Here the Bash removes extra spaces in the text because in the first case the entire text is taken as a "single" argument and thus preserving extra spaces.
But in the second case echo
command receives the text as 5 arguments.
Quoting the variable will also be helpful while passing arguments to commands
In the below command, echo
only gets single argument as "Hello World"
$ variable="Hello World"
$ echo "$variable"
But in case of the below scenario echo
gets two arguments as Hello
and World
$ variable="Hello World"
$ echo $variable