0

I have a script that is used to set up a couple default widgets on a wordpress site through wp-cli.

script widgets.sh:

read -r -d '' TEXT << EOM
<strong>Line1</strong>
 
Address
 
Phone: 555-x
E-mail: so@you.say
EOM

sudo --user=$USRNAME -s wp widget add text footer-1 999 --title="Widget1" --filter="true" --visual="true" --text="$TEXT"

problem:

My problem lies in that when i call the shell script with these commands in them, such as by running

bash ~/widgets.sh

the variable $TEXT once sent to wp-cli have its linebreaks removed. If i add

echo "$TEXT"

to the script file, it echoes it with linebreaks preserved properly, which just adds to my confusion.

If i copy and paste the commands into my terminal, it preserves the linebreaks from the HEREDOC properly.

I do not understand why, but there is a lot of things i don't understand about shell scripts and bash in general, so i hope someone can enlighten me :)

Thanks in advance!

SL5net
  • 2,282
  • 4
  • 28
  • 44
Ronin
  • 65
  • 1
  • 9
  • 1
    Would be helpful if the sentences were a bit shorter. And leave out: "My problem is". Use for e.g. "Problem:", and "Expected:", "Result:" – SL5net Nov 02 '20 at 11:42
  • Thanks for the heads up @SL5net - I'l try to be more concise! – Ronin Nov 03 '20 at 06:34

1 Answers1

0

In the html context, line numbers in the source code are not displayed on the rendered pages. If you add
tags to the end of your lines, your line breaks should show up there again.

May it help you to use nl2br

SL5net
  • 2,282
  • 4
  • 28
  • 44
  • 1
    It is supposed to be saved in the database with linebreaks and not
    , as i believe it is run through nl2br or similar when needed. Although, i will give it a try, if it works, it works, and thats good enough for me for now
    – Ronin Nov 06 '20 at 10:12
  • 1
    Just gave it a test, and it works fine. I just have one last question regarding this though, as while it does solve my problem, it does not really answer the question as to why / how to void it from working differently when pasted into terminal than executing it as a shell script. I can accept your answer if you want, as you did solve my problem after all. Oh and thanks for that :) – Ronin Nov 06 '20 at 10:17