# How do do more complex shell commands in Sage?

According to the tutorial, I can do things like !ls inside the Sage shell (in fact, here ls works).

But

echo $PATH  doesn't work whether I prepend a !, put it in quotes, whatever. How do I get such things to work, if at all? Is PATH just not defined in the Sage session? (Secondarily, I'm trying to figure out how to put things in a path of a Sage session; sys.path seems to be the place to do it, but I'm not 100% sure of that.) edit retag close merge delete ## 3 answers Sort by » oldest newest most voted The exclamation point syntax comes from IPython: see their documentation. In particular, in a line starting with !, a single dollar sign indicates a python expression to be expanded, while if you want to pass an actual dollar sign to the shell, use a double dollar sign: sage: PATH = 33 sage: !echo$PATH
33
sage: !echo PATH
/Applications/sage/local/bin:/Applications/sage:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/palmieri/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/texbin:/usr/local/bin


(I didn't know this until I read the IPython docs just now...)

For the second question, you can manipulate the current environment using os.environ, which is a dictionary. For example, os.environ['PATH'] is the current path, which you can modify however you want. I don't know if there are other good ways to modify the path in Python.

more

Yeah, guess I should have looked at their docs. Well, too lazy, and it might have taken me quite a while to find out where this actually lived.

( 2011-08-10 10:24:33 -0600 )edit

As for the path, I'm not even sure anymore which path Sage is searching through. I think it's PATH, but then what is sys.path?

( 2011-08-10 10:25:05 -0600 )edit

According to http://docs.python.org/library/sys.html#sys.path, sys.path is where Python looks for modules. It's not the path used by shell commands. Some experiments indicate that os.environ['PATH'] is what Sage uses -- I added a new directory to it, created a simple executable shell script in that directory, and it ran with "!new-script". Without modifying os.environ['PATH'], it didn't run: "sh: new-script: command not found".

( 2011-08-10 11:01:56 -0600 )edit

And presumably PYTHONPATH is even less relevant... interesting.

( 2011-08-11 06:07:52 -0600 )edit

sys.path is determined from PYTHONPATH.

( 2011-08-12 05:23:19 -0600 )edit

This might help:

http://stackoverflow.com/questions/89...

For getting and altering the path, the python os module is useful:

http://docs.python.org/library/os.htm...

more

That was helpful for the path - I had forgotten about os, only thought about sys.

( 2011-08-10 10:25:45 -0600 )edit

This may be entirely newbish, but I was under the impression any terminal command can be run inside the sage console.

For example:

sage: cd ~/Documents
/home/steven/Documents
sage: pwd
'/home/steven/Documents'
sage: mkdir test
sage: ls
1977_001.pdf      do_my_stuff.m        matlab@               test/
batch_script      Dropbox/         MATLAB/               test.pdf
caq_letter_2.odt  FIELDS_stuff/        passport_photo.jpg        test.svg
CAQ_letter.odt    french_passport.jpg  passport_plus_income_tax.pdf
costco_runs.xls   income_tax_1.pdf     research_funding/
sage: rm -r test
rm: remove directory test'? y
sage: ls
1977_001.pdf      do_my_stuff.m        matlab@               test.pdf
batch_script      Dropbox/         MATLAB/               test.svg
caq_letter_2.odt  FIELDS_stuff/        passport_photo.jpg
CAQ_letter.odt    french_passport.jpg  passport_plus_income_tax.pdf
costco_runs.xls   income_tax_1.pdf     research_funding/
`

I don't really know how echo works, so I can't really help you there.

more

As far as I can tell, the presence of "ls", etc., is because of IPython. These commands are set up in SAGE_ROOT/local/lib/python/site-libraries/IPython/iplib.py. That file defines commands ls, cp, mv, rm, cd, and others, but not every terminal command: try running "python" or "gcc" or "echo" and you'll get an error.

( 2011-08-13 04:54:43 -0600 )edit