jcalfee314
jcalfee314

Reputation: 4830

turn off bash variable substitution

function ctrace {
  echo "+ $@"
  "$@"
}

ctrace echo "hi"

How would I get this function to output (with quotes):

echo "hi"

In this version the quotes are lost echo hi... Here is another example:

a=b
ctrace echo $a

This should output echo $a instead of echo b

Upvotes: 0

Views: 213

Answers (2)

Tuxdude
Tuxdude

Reputation: 49473

You need to enclose the string within single quotes

ctrace 'echo "hi"'
ctrace 'echo $a'

Upvotes: 0

cdarke
cdarke

Reputation: 44354

The problem is not the function, but the caller.

In the first case the quotes are stripped out before the function gets the parameters. In the second, $a substitution is done before it gets to the function.

Try:

ctrace 'echo "hi"'
ctrace 'echo $a'

Upvotes: 1

Related Questions