0

I stumbled across this odd behavior by accident while working on a project. Here is the demo code that will reproduce the same issue -

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse

parser = argparse.ArgumentParser(description='Random observations')

parser.add_argument('-g', '--get', help='Get n of something')
parser.add_argument('-c','--create', help='Create something')

args = vars(parser.parse_args())

if args['get']:
    print(f"Retrieved {args['get']} resources...")

if args['create']:
    print(f"Created resource..{args['create']}")

I made the script executable just for brevity using chmod +x filename
Now, this is where it gets wierd. If i run the script like this -
$ ./so -c "Calling `./so -g 5`"
I get the output as -
Created resource..Calling Retrieved 5 resources...
Why is the command in string getting executed, shouldn't it just go in as a string and come out as a string ?

I did a few more experiments and here are my findings:
Command -
$ ./so -c """Calling `./so -g 5`"""
Output
Created resource..Calling Retrieved 5 resources...

Command -
$./so -c '''Calling `./so -g 5`'''
Output -
Created resource..Calling `./so -g 5`

Why is the last one working as expected and not the others?

P.S I do not know the necessary tags for this question, please feel free to edit it.

jar
  • 2,646
  • 1
  • 22
  • 47
  • https://unix.stackexchange.com/questions/27428/what-does-backquote-backtick-mean-in-commands – user2357112 Nov 04 '18 at 18:53
  • You seem to be expecting shell quotation to work like Python string literals. It doesn't. – user2357112 Nov 04 '18 at 18:54
  • Try echo rather than your script. – DisappointedByUnaccountableMod Nov 04 '18 at 18:56
  • @user2357112 Thank you for letting me know what I was actually doing. However, that still doesn't clarify why the last one doen't output the same as the ones before if everything inside backtick is supposed to be executed? I have edited the question to now reflect its opposite behavior. – jar Nov 04 '18 at 19:01
  • https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – user2357112 Nov 04 '18 at 19:02
  • @user2357112 That explains it. Would you like to answer this question? I will accept it. Or should i delete it? – jar Nov 04 '18 at 19:06
  • In a case like this I posters to print `sys.argv` to see what strings get passed into Python, and `args` (before or after `vars`) to see exactly what the parser produced. – hpaulj Nov 04 '18 at 19:14

0 Answers0