6
6
6elkka2016-12-23 17:27:27
bash
6elkka, 2016-12-23 17:27:27

Pretty way to specify string literals with control characters in bash?

It is required to use a string literal in bash containing, for example, a tab or newline. Here are the options I found.
Option 1:

x="hello
world"

Option 2:
x=`echo -e "hello\nworld"`

Can it be done differently?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
6
6elkka, 2017-01-06
@6elkka

The answer was found in man bash.
You need to escape the string with single quotes and put $ in front of it:
x=$'a\nb'

S
sim3x, 2016-12-23
@sim3x

www.stackoverflow.com/a/1168084

VAR=$(cat <<'END_HEREDOC'
abc'asdf"
$(dont-execute-this)
foo"bar"''
END_HEREDOC
)

echo "$VAR"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question