bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: consistency probs var & function re-use


From: L A Walsh
Subject: Re: consistency probs var & function re-use
Date: Sun, 11 Jun 2017 14:57:43 -0700
User-agent: Thunderbird



Chris F.A. Johnson wrote:
On Fri, 9 Jun 2017, L A Walsh wrote:
----
  First problem: If you are assigning a string to a variable,
you need to put quotes around the string.

   You don't need to quote it unless it contains literal whitespace.
---
   Not true if you want to reproduce the string as output by
"declare".  Since declare doesn't output the literal value in a
variable, but an expanded one, you need to put quotes around any
var that you intend to expand with 'declare'.

Greg's example:

  imadev:~$ x=$'foo\nbar'
  imadev:~$ declare -p x
  declare -- x="foo
  bar"

Shows that "declare" isn't quoting its output such that it can be used for assignment to a var, or as 'read' input.

To do that, you can use "printf %q" -- as in:

 x=$'foo\nbar'
 printf -v qx "%q" "$x"
 printf "%s\n" "$qx"
 $'foo\nbar'

I used double quotes around the original:

 x="$'foo\nbar'"

Which gives:

 printf "%s\n" "$x"
 $'foo\nbar'

The exact same output.

I.e. you need to double quote Greg's input for it to be re-usable as input.













reply via email to

[Prev in Thread] Current Thread [Next in Thread]