using python at the bash shell?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    using python at the bash shell?

    Hi all. I just installed Ubuntu and I'm learning how to use the bash
    shell. Aside from the normal commands you can use, I was wondering if
    it's possible to use Python from the terminal instead of the normal bash
    commands (e.g. print instead of echo). My main reason for asking is that
    I like using Python for everything, and if I don't need to learn the
    bash 'language', then I won't just yet.

    Thanks.
  • John McMonagle

    #2
    Re: using python at the bash shell?

    On Mon, 2006-08-07 at 21:03 -0400, John Salerno wrote:
    Hi all. I just installed Ubuntu and I'm learning how to use the bash
    shell. Aside from the normal commands you can use, I was wondering if
    it's possible to use Python from the terminal instead of the normal bash
    commands (e.g. print instead of echo).
    >From a terminal window typing,
    python -c "print 'testing'"

    would produce the same results as

    echo 'testing'

    in bash shell, but obviously not as nice.
    My main reason for asking is that
    I like using Python for everything, and if I don't need to learn the
    bash 'language', then I won't just yet.
    >
    Just type python and do your stuff in the python interpreter. This
    becomes your shell.

    This then got me thinking, how about making python your login shell. So
    I modified my shell login entry in /etc/passwd to /usr/bin/python,
    logged in and voila, I was presented with my friendly python shell upon
    opening a terminal window.

    So the answer is "Yes".





    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.

    Comment

    • skip@pobox.com

      #3
      Re: using python at the bash shell?


      JohnAside from the normal commands you can use, I was wondering if
      Johnit's possible to use Python from the terminal instead of the
      Johnnormal bash commands (e.g. print instead of echo).

      Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
      you've asked for, but it provides some features that help integrate Python
      with the shell environment.

      Skip

      Comment

      • Simon Forman

        #4
        Re: using python at the bash shell?

        skip@pobox.com wrote:
        JohnAside from the normal commands you can use, I was wondering if
        Johnit's possible to use Python from the terminal instead of the
        Johnnormal bash commands (e.g. print instead of echo).
        >
        Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
        you've asked for, but it provides some features that help integrate Python
        with the shell environment.
        >
        Skip
        FWIW, I second this. If you want to use python as your shell use
        Ipython. It's just great. Fernando Pérez has done a Good Thing IMHO.

        Be sure to read (or at least skim) the manual:


        Here's an example of calling "man ls" and then getting the results of
        "ls -l" into a python list (one list item per line) and then extracting
        the size (as a string) of the MANIFEST file... Enjoy... (I hope
        reformatting doesn't screw this up too badly.)


        $ ipython
        Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
        Type "copyright" , "credits" or "license" for more information.

        IPython 0.7.1.fix1 -- An enhanced Interactive Python.
        ? -Introduction to IPython's features.
        %magic -Information about IPython's 'magic' % functions.
        help -Python's own help system.
        object? -Details about 'object'. ?object also works, ?? prints more.

        In [1]: !man ls
        Reformatting ls(1), please wait...
        <viewing actual man page here. ~Simon>

        In [2]: !!ls -l
        Out[2]:
        ['total 40',
        'drwxr-xr-x 3 sforman sforman 4096 2006-08-02 12:41 docs',
        'drwxr-xr-x 3 sforman sforman 4096 2006-04-27 23:49 logs',
        '-rw-r--r-- 1 sforman sforman 887 2006-04-27 23:49 MANIFEST',
        '-rw-r--r-- 1 sforman sforman 173 2006-05-04 14:04 MANIFEST.in',
        '-rw-r--r-- 1 sforman sforman 3338 2006-06-14 20:19 README',
        '-rw-r--r-- 1 sforman sforman 475 2006-07-16 17:40 setup.py',
        'drwxr-xr-x 4 sforman sforman 4096 2006-08-07 15:04 src',
        'drwxr-xr-x 3 sforman sforman 4096 2006-06-21 20:59 tests',
        '-rw-r--r-- 1 sforman sforman 898 2006-07-09 21:53 TODO',
        'drwxr-xr-x 3 sforman sforman 4096 2006-08-05 17:37 util']

        In [3]: data = [n.split() for n in _[1:]]

        In [4]: print data[2][4]
        887

        In [5]:


        etc...

        HTH,
        ~Simon

        Comment

        • BartlebyScrivener

          #5
          Re: using python at the bash shell?


          John Salerno wrote:
          I like using Python for everything, and if I don't need to learn the
          bash 'language', then I won't just yet.
          And like vim, Ipython works on both windows and ubuntu.

          rd

          Comment

          • John Salerno

            #6
            Re: using python at the bash shell?

            skip@pobox.com wrote:
            JohnAside from the normal commands you can use, I was wondering if
            Johnit's possible to use Python from the terminal instead of the
            Johnnormal bash commands (e.g. print instead of echo).
            >
            Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
            you've asked for, but it provides some features that help integrate Python
            with the shell environment.
            >
            Skip
            Can you use IPython to do normal bash things, like installing, etc.?

            Comment

            • Thomas Bartkus

              #7
              Re: using python at the bash shell?


              "John Salerno" <johnjsal@NOSPA Mgmail.comwrote in message
              news:44d7e364$0 $24980$c3e8da3@ news.astraweb.c om...
              Hi all. I just installed Ubuntu and I'm learning how to use the bash
              shell. Aside from the normal commands you can use, I was wondering if
              it's possible to use Python from the terminal instead of the normal bash
              commands (e.g. print instead of echo). My main reason for asking is that
              I like using Python for everything, and if I don't need to learn the
              bash 'language', then I won't just yet.
              The answer is yes and you are getting excellent tips from others.

              I am just validating your experience by saying that coming from Python is a
              barrier to my learning BASH. The more I work with Linux/BASH, the more I
              see how I might have used BASH to script something I have already done in
              Python. But the question that always comes up is why bother with BASH at
              all ;-) And with Python available everywhere, ....

              I've just reconsiled to pick up my BASH by osmosis and concentrate on
              (much!) cleaner scripting with Python.

              Thomas Bartkus


              Comment

              • John Salerno

                #8
                Re: using python at the bash shell?

                Thomas Bartkus wrote:
                I am just validating your experience by saying that coming from Python is a
                barrier to my learning BASH. The more I work with Linux/BASH, the more I
                see how I might have used BASH to script something I have already done in
                Python. But the question that always comes up is why bother with BASH at
                all ;-) And with Python available everywhere, ....
                >
                I've just reconsiled to pick up my BASH by osmosis and concentrate on
                (much!) cleaner scripting with Python.
                Glad to know I'm not alone! But I'm certainly much happier using Python
                than bash anyway. All I really want to learn as far as the shell is
                concerned are the 'natural' things you can do, such as directory/file
                manipulation, installing programs, etc. I don't have much of a need to
                learn the actual programming parts of the bash language, especially when
                I can do the same with Python, which is so much cooler. :)

                Comment

                • skip@pobox.com

                  #9
                  Re: using python at the bash shell?

                  >I've just reconsiled to pick up my BASH by osmosis and concentrate on
                  >(much!) cleaner scripting with Python.
                  JohnI don't have much of a need to learn the actual programming parts
                  Johnof the bash language, ...

                  As long as you promise never, ever, ever to try programming in csh...

                  Skip

                  Comment

                  • John Salerno

                    #10
                    Re: using python at the bash shell?

                    skip@pobox.com wrote:
                    >I've just reconsiled to pick up my BASH by osmosis and concentrate on
                    >(much!) cleaner scripting with Python.
                    >
                    JohnI don't have much of a need to learn the actual programming parts
                    Johnof the bash language, ...
                    >
                    As long as you promise never, ever, ever to try programming in csh...
                    >
                    Skip
                    Heh heh, Python all the way!

                    Comment

                    • Simon Forman

                      #11
                      Re: using python at the bash shell?

                      John Salerno wrote:
                      skip@pobox.com wrote:
                      JohnAside from the normal commands you can use, I was wondering if
                      Johnit's possible to use Python from the terminal instead of the
                      Johnnormal bash commands (e.g. print instead of echo).

                      Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
                      you've asked for, but it provides some features that help integrate Python
                      with the shell environment.

                      Skip
                      >
                      Can you use IPython to do normal bash things, like installing, etc.?
                      "normal bash things"? :-) Yes, most commands can be run by putting an
                      '!' before them. If you ever need to run something that for some
                      reason doesn't work with this, you can always run !bash and do it in
                      bash. :-)

                      Peace,
                      ~Simon

                      Comment

                      • John Salerno

                        #12
                        Re: using python at the bash shell?

                        Simon Forman wrote:
                        "normal bash things"? :-)
                        forgive my ignorance, i just installed linux on saturday! :)

                        Comment

                        • Tim Roberts

                          #13
                          Re: using python at the bash shell?

                          John Salerno <johnjsal@NOSPA Mgmail.comwrote :
                          >
                          >Can you use IPython to do normal bash things, like installing, etc.?
                          Most scripts on Linux have a "hash-bang" line as their first line:
                          #! /bin/sh

                          When you execute that script, the system knows that it has to load sh or
                          bash to process it, regardless of what program launched the script. The
                          same thing works for Python scripts:
                          #! /usr/bin/python
                          --
                          - Tim Roberts, timr@probo.com
                          Providenza & Boekelheide, Inc.

                          Comment

                          • cga2000

                            #14
                            Re: using python at the bash shell?

                            On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
                            John Salerno wrote:
                            skip@pobox.com wrote:
                            JohnAside from the normal commands you can use, I was wondering if
                            Johnit's possible to use Python from the terminal instead of the
                            Johnnormal bash commands (e.g. print instead of echo).
                            >
                            Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
                            you've asked for, but it provides some features that help integrate Python
                            with the shell environment.
                            >
                            Skip
                            Can you use IPython to do normal bash things, like installing, etc.?
                            >
                            "normal bash things"? :-) Yes, most commands can be run by putting an
                            '!' before them. If you ever need to run something that for some
                            reason doesn't work with this, you can always run !bash and do it in
                            bash. :-)
                            >
                            Sorry, I'm late with my howmework .. but this is rather cool.

                            Although .. prefixing all your "system commands" with the far-to-reach
                            "!" would probably become a royal pain after a while.

                            Why do you write "most commands" .. what type of command might not be
                            run by "putting '!' before them?"

                            In the linux world it would be rather interesting if a distro was
                            available that uses nothing but python.

                            The installer .. the startup scripts .. utilities .. etc.

                            Maybe there is such a thing and I'm just not aware of it?

                            Since it would be byt-code being executed it would be faster than
                            regular shell stuff and a lot easier to customize/maintain.

                            Don't know enough to figure out if such a thing is possible, though ..

                            Thanks

                            cga

                            Comment

                            • Simon Forman

                              #15
                              Re: using python at the bash shell?

                              cga2000 wrote:
                              On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
                              John Salerno wrote:
                              skip@pobox.com wrote:
                              JohnAside from the normal commands you can use, I was wondering if
                              Johnit's possible to use Python from the terminal instead of the
                              Johnnormal bash commands (e.g. print instead of echo).

                              Take a look at ipython <http://ipython.scipy.o rg/>. It's not precisely what
                              you've asked for, but it provides some features that help integrate Python
                              with the shell environment.

                              Skip
                              >
                              Can you use IPython to do normal bash things, like installing, etc.?
                              "normal bash things"? :-) Yes, most commands can be run by putting an
                              '!' before them. If you ever need to run something that for some
                              reason doesn't work with this, you can always run !bash and do it in
                              bash. :-)
                              Sorry, I'm late with my howmework .. but this is rather cool.
                              >
                              Although .. prefixing all your "system commands" with the far-to-reach
                              "!" would probably become a royal pain after a while.
                              I use my computer almost exclusively for programming, so I used xmodmap
                              to remap the number & symbol keys the other way round, i.e. I get
                              "!@#$%^&*() " from just pressing the keys and "1234567890 " when I press
                              shift+keys. It's soooo much nicer (except when I use another
                              machine...) So hitting '!' for me at least ain't so bad. ;P YMMV

                              I don't actually use Ipython as my shell, but I'd *like* to..
                              >
                              Why do you write "most commands" .. what type of command might not be
                              run by "putting '!' before them?"
                              Well, I wrote that just to hedge my bets, but here's an example: I set
                              up lesspipe and source-highlight to add syntax highlighting to various
                              file types when viewed through less. But using "!less neat.py" in
                              Ipython doesn't work because (I'm guessing here) Ipython doesn't handle
                              the color escape codes. It winds up looking like this:

                              ESC[31m#!/usr/local/bin/python2.5ESC[m
                              ESC[01;34mfromESC[m random ESC[01;34mimportESC[m random as f


                              ESC[01;34mdefESC[m
                              ESC[01;30mgESC[mESC[31m(ESC[mESC[31m)ESC[mESC[31m:ESC[m
                              n1 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
                              ESC[01;34mwhileESC[m TrueESC[31m:ESC[m
                              n2 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
                              yield n1 ESC[31m-ESC[m n2
                              n1 ESC[31m=ESC[m n2

                              Ew.

                              So in this case I'd have to "!bash" then "less neat.py"...
                              In the linux world it would be rather interesting if a distro was
                              available that uses nothing but python.
                              >
                              The installer .. the startup scripts .. utilities .. etc.
                              >
                              Maybe there is such a thing and I'm just not aware of it?
                              IMHO it would be neat, but it'd be kind of a "stunt". There's a ton
                              of excellent code in most any linux distro *not* written in python.
                              Since it would be byt-code being executed it would be faster than
                              regular shell stuff and a lot easier to customize/maintain.
                              Mmmm... I bet it'd be hard to beat, say, grep... Or any of the
                              small, custom-purpose C-coded tools. (then there's make... gcc...
                              not easy to rewrite those.. just off the top of my head...)
                              >
                              Don't know enough to figure out if such a thing is possible, though ..
                              >
                              Thanks
                              >
                              cga
                              Peace,
                              ~Simon

                              Comment

                              Working...