Ask Your Question
1

How do do more complex shell commands in Sage?

asked 2011-08-09 12:38:25 +0100

kcrisman gravatar image

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 flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
4

answered 2011-08-10 01:36:10 +0100

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.

edit flag offensive delete link more

Comments

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.

kcrisman gravatar imagekcrisman ( 2011-08-10 17:24:33 +0100 )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?

kcrisman gravatar imagekcrisman ( 2011-08-10 17:25:05 +0100 )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".

John Palmieri gravatar imageJohn Palmieri ( 2011-08-10 18:01:56 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2011-08-11 13:07:52 +0100 )edit

sys.path is determined from PYTHONPATH.

John Palmieri gravatar imageJohn Palmieri ( 2011-08-12 12:23:19 +0100 )edit
1

answered 2011-08-09 13:56:58 +0100

benjaminfjones gravatar image

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...

edit flag offensive delete link more

Comments

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

kcrisman gravatar imagekcrisman ( 2011-08-10 17:25:45 +0100 )edit
0

answered 2011-08-12 21:51:54 +0100

StevenPollack gravatar image

updated 2011-08-12 21:52:33 +0100

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.

Does this help/address your question?

edit flag offensive delete link more

Comments

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.

John Palmieri gravatar imageJohn Palmieri ( 2011-08-13 11:54:43 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-08-09 12:38:25 +0100

Seen: 1,331 times

Last updated: Aug 12 '11