8

How do I execute a shell command from Ipython/Jupyter notebook passing the value of a python string variable as a string in the bash argument like in this example:

sp_name = 'littleGuy' #the variable

sp_details = !az ad app list --filter "DisplayName eq '$sp_name'" #the shell command

I've tried using $sp_name alone, ${sp_name}, {sp_name} etc as outlined in this related question, but none have worked.

The kicker here is the variable name needs to be quoted as a string in the shell command.

EDIT1:

@manu190466. I was judging from the string output that your solution worked. It appears for some reason it does not in practice. I wonder if az ad app list URL encodes the query or something...?

Thoughts?

enter image description here

3 Answers 3

14
+50

The main problem you encounters seems to come from the quotes needed in your string. You can keep the quotes in your string by using a format instruction and a raw string.

Use a 'r' before the whole string to indicate it is to be read as raw string, ie: special caracters have to not be interpreted. A raw string is not strictly required in your case because the string constructor of python is able to keep single quotes in a double quotes declaration but I think it's a good habit to use raw string declarators when there are non alphanumerics in it.

There are at least two way to format strings :

Older method herited from ancient langages with % symbols:

sp_name = 'littleGuy' #the variable
sp_query = r"DisplayName eq '%s'"%(sp_name) 

sp_details = !az ad app list --filter {sp_query}

Newer method with {} symbols and the format() method :

sp_name = 'littleGuy' #the variable
sp_query = r"DisplayName eq '{}'".format(sp_name) 

sp_details = !az ad app list --filter {sp_query}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked. Thank you. sp_query = r"DisplayName eq '{}'".format(sp_name), sp_query, "DisplayName eq 'python-sp'"
Is this supposed to work in the pure ipython shell (CLI, not the Jupyter notebook) as well? It didn't seem to for me, but maybe that's because I had some extra quoting in my case.
0

would you try something like this:

sp_name = 'littleGuy' #the variable
sp_query = "DisplayName eq "+sp_name 

sp_details = !az ad app list --filter {sp_query}

3 Comments

Same error as before. The sp_name needs additional quotes within the DisplayName eq string. Should be "DisplayName eq 'littleGuy'". sp_name = 'littleGuy' sp_query = "DisplayName eq " + sp_name sp_details = !az ad app list {sp_query} > sp_details2.json sp_details ['ERROR: az: error: unrecognized arguments: DisplayName eq littleGuy', 'usage: az [-h] [--verbose] [--debug]', ' [--output {json,jsonc,table,tsv,yaml,none}] [--query JMESPATH]', ' {ad} ...']
what if you add quotation to you sp_name? like "'littleGuy'"
Tried sp_name = "littleGuy", sp_name = "'littleGuy'" and sp_name = '"littleGuy"'. Same error result.
0

The issue can be also resolved using something like below

sp_name = 'littleGuy' #the variable
sp_details = !az ad app list --filter "DisplayName eq '{sp_name}'"

6 Comments

Hm. I still receive an error on this one ERROR: az: error: unrecognized arguments: eq \'littleGuy\'"',
@SeaDude, can you comment what command you run when you are using a bash terminal
Sure. sp_name = 'littleGuy' sp_details = !az ad app list --filter \"DisplayName eq \'{sp_name}\'\" sp_details
Not this. Open a terminal and run the command in a bash and tell me what command you ran. Without iPython
IPython is the subject of this question as well as my only python env. I'm running terminal commands from IPython, with no alternatives, in this case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.