質問

I don't understand what the ${} is doing in the fourth and sixth prompt, and I can't find any documentation about this, The book Python for Unix and Linux System Administrator has an example like the one in the sixth prompt, in which the variable is not only prepended of $ but also enclosed in {} in order to embed the string inside another one, It works out if I quote the complete expression, but what's happening in [4 & 6]?:

In [1]: test = 'TEST TEST TEST!!!'                                                                                          

In [2]: !echo test
test                                                                                                                         

In [3]: !echo $test                                                                                                         
TEST TEST TEST!!!                                                                                                            

In [4]: !echo ${test}   # Don't understand this one.                                                                                                
TEST TEST!!!  

In [5]: !echo {test}                                                                                                       
TEST TEST TEST!!!

In [6]: !echo ${test}TEST!!!   # Nor this one either.
TEST TEST!!!TEST!!!

In [7]: !echo '${test}TEST!!!'
$TEST TEST TEST!!!TEST!!!
役に立ちましたか?

解決

In[4], typo, ) instead of } , otherwise I would guess that when dooing ${test}, {test} is expended to TEST TEST TEST, leading to $TEST TEST TEST , and then $TEST is expended again (in shell I suppose).

I say that because :

In [1]: test = 'PATH TEST TEST!!!'

In [2]: !echo "${test}"
/usr/local/bin:/usr/local/share/pyt.../.rvm/bin TEST TEST!!!

But you have to be carefull, IPython does its own variable expansion, $foo or {foo} (no dollar sign) expand to Python value of foo. Wherease Shell often use $FOO or ${FOO} (dollar AND Bracket).

In IPython, when using ! you have to use double dollar $$ for explicit dolar. not sure how to escape Brackets though.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top