0

I basically need to load, save and reload multiple times and in different forms a variable that contains arguments for a program.

The problem is that this variable can't be treated as a usual variable because it contains the "-" sign in the start. And print it or load it to be possible to see what it contains makes things hard due to the constant need of a \\ at the start before it can be used or not.

A trick was always saved it with the \\, but before reading it, I echo $it then read the last output as a variable for the use case.

This is strongly impractical, so I need a way to save the contents of a variable in a manner that will never interfere with the code that executes it.

The below variable is used by yad a default for gdb, but it can be for any other program.

For example, a quick test to know if a variable like that can be used in my context is trough printf WITHOUT the need of any printf parameter. This is because:

gdbParameters="-ex 'set pagination off' -ex 'set environment G_MESSAGES_DEBUG = all'"
printf $gdbParameters

returns:

printf: usage: printf [-v var] format [arguments]

Thank you all.

DATALOT
  • 457

2 Answers2

4

The whole point of printf is that you don't use it to print the variable directly but instead you pass it a format and then the variable as an argument:

$ gdbParameters="-ex 'set pagination off' -ex 'set environment G_MESSAGES_DEBUG = all'"
$ printf '%s\n' "$gdbParameters"
-ex 'set pagination off' -ex 'set environment G_MESSAGES_DEBUG = all'

As for saving a variable in a way that will never interfere with any code that tries to use it, that is impossible. Instead, you need to take care and always write good, clean code that can deal with arbitrary content in a variable.

That's where basic good practices such as always quoting your variables (see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells) and using the standard -- to indicate the end of options whenever you call a program and pass it a variable as input (e.g. grep -- "$variable" file).

terdon
  • 242,166
0

If your shell is POSIX compliant (in case that printf is a builtin command) or calls a compliant printf, just try:

printf -- "$var"

This tells printf not to interpret a following argument as option.

In general however keep in mind that a percent or a backslash in the content of $var may cause your output to be modified in a way that you do not expect. For this reason,

printf "%s" "$var"

is the better solution.

schily
  • 19,173
  • This works, but I can't tell to yad that -- – DATALOT Jun 20 '20 at 17:47
  • If you cannot change the script that calls printf, is this because it is from someone else? Did you think about making a bug report? – schily Jun 20 '20 at 17:50
  • yad is Yet Another Dialog application for Linux systems with GTK. It's not a script I think, but I need to insert and parse a yad --form --"GDB Parameters":TXT. So basically I need to parse a text field input from the user and store it to read it again to be able to set it to the same field. This is because every time user opens my script, it needs to see in the field the last thing it puts. – DATALOT Jun 20 '20 at 17:55
  • If you write an own script, you can do whatever you like, so I see no problem. – schily Jun 20 '20 at 18:02