0

When I run the following commands somehow single quotation marks are added to the http links

s="https://v.h.ghg?ff"
os="Linux"
echo $s
(set -x; gter --source $s --os $os)

output is

https://v.h.ghg?ff
----
+ gter --source 'https://v.h.ghg?ff' --os Linux

Why is this happening. And how could we prevent this from happening

Malik
  • 31
  • 2
  • 3
    That's just how `set -x` works -- if a string contains special characters, it's put in quotes. – Barmar Feb 25 '20 at 00:44
  • `?` is a special character, it's a wildcard that matches any character (like `.` in regular expressions). – Barmar Feb 25 '20 at 00:44
  • 1
    You should put all your variables in double quotes. Otherwise, if you have a filename that matches the wildcard `https://v.h.ghg?ff` then `$s` will be replaced with that filename. – Barmar Feb 25 '20 at 00:45
  • 1
    Why is this a problem? It's just debugging output, and the quotes allow you to paste the output to get the same result as the command itself. – Barmar Feb 25 '20 at 00:47
  • Think of it like `repr()` in Python adding quotes and escape sequences to make your string print as code. It's not changing the string's value, it's just printing it in an unambiguous way. – Charles Duffy Feb 25 '20 at 01:05
  • BTW, it's helpful to have a mental distinction between syntax (as the quotes are) and data (the *actual content* for which the quotes are instructions on how to parse). For a discussion of other problems caused by that same (lack of) distinction, see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy Feb 25 '20 at 01:10
  • I thought the value of the string is inherently different, that's why it's being printed differently. A command that i am using is failing complaining about the link provided and I thought this might be because somehow the single quotation marks get added – Malik Feb 25 '20 at 01:12
  • 1
    ...and for automated checking for issues like missing quotes, see http://shellcheck.net/ (with the caveat that it's not always as thorough as it should be; see f/e [BashPitfalls #14](https://mywiki.wooledge.org/BashPitfalls#echo_.24foo) on why `echo $s` is dangerous. – Charles Duffy Feb 25 '20 at 01:12
  • Keep in mind that the *program itself* doesn't see the quotes -- all argument lists in UNIX are provided to the called program as an array of C strings. Thus, whether you run it as `foo --bar baz` or `foo "--bar" 'baz'`, the actual argument list is `char**{"foo", "--bar", "baz", NULL}` (which is to say, an array with `foo` as the first entry, `--bar` as the second, `baz` as the third, and a terminating NUL), with only the literal data inside the array and the quotes already removed by the calling shell before it invoked the `execve()` syscall to hand over control to the program being started. – Charles Duffy Feb 25 '20 at 01:13

0 Answers0