1

I have a script that takes text as an argument and encodes it. If the text contains only single quotes I surround it with double and vice versa, but it gets tricky if it contains both because it will be closed at first appearance.

script "I can't use "test" text" // Double quote will be terminated at "I can't use " 

So how can I escape both cases without using a backslash, like in Python using triple single, or double quotes.

script '''I can't use "test" text''' // or script """I can't use "test" text""" 
10
  • 1
    There's nothing like triple quotes or raw strings in bash. Why without using backslash? Commented Jul 11, 2022 at 19:03
  • I may pass huge text which I will copy/paste from somewhere. And adding backslashes to every quote isn't easy. Commented Jul 11, 2022 at 19:05
  • 1
    So read the text from stdin rather than making it an argument. Commented Jul 11, 2022 at 19:06
  • you could put it in a file. Commented Jul 11, 2022 at 19:06
  • 1
    @KamilCuk If you type echo "I can't use "test" text" you won't see the quotes around test Commented Jul 11, 2022 at 19:09

1 Answer 1

2

Use a here-document, and use command subsitution to turn it into an argument.

script "$(cat <<'EOF' I can't use "test" text EOF)" 

Or

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.