Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been studying bash for the past three weeks or so, and it is very easy to learn since it is basically just command line inputs. I've decided to now move onto python but I've been lost the whole time trying to learn it. My question is in python, it all seems to revolve around numbers, lists, variables, etc. While bash seems to be much more straight forward with the commands. I can not find one example in python where there is a way to move files, change directories or other things like that. I feel like I'm already too used to bash to learn python. Can someone please explain any of this to me.

share|improve this question
    
a helpful question would be why you are learning? –  marinara Mar 7 '12 at 19:24
add comment

2 Answers 2

Bash is not a programming language, but a shell targetted for quickly entering commands and executing those. Therefore, you run other commands doing your job (e.g. mv moves files and directories). Variables in Bash are limited and since commands are all strings, there is no reason to be very strict on it. (it is possible though to force types, see help declare).

Python on the other hand is a more extensive programming language with more language features. Instead of executing other programs, you use built-in functions or other functionality from modules (libraries in other languages). For a basic tutorial on input/output in Python, see http://docs.python.org/tutorial/inputoutput.html. The whole tutorial itself is worth reading if you want to start programming in Python.

share|improve this answer
add comment

Python is a program language; and like all other programming languages, it's designed to describe process and data. Most of your questions deal with the API to the operating system in the os module, for example, os.chdir() and os.rename() (for the UNIX mv command).

The shell is an operating environment, meant to give you easy access to the operating system at various levels (files, processes, etc.). It includes programming constructs like variables, loops, and conditionals, so in this sense it is a programming language, but it was not designed to be one - that evolved. You can do quite amazing things with a shell script - I once wrote a call tracking system that handled 60k requests a month. But it also has limitations that a computer language can handle better.

For example, you could write a web browser in Bash, but it would be inefficient, especially when trying to concurrently download the html, css and javascript pages and handle user input. A programming language like Python, which has access to threads, would handle this more efficiently.

While Python has a "shell" as well, it's not one that's easy to use for accessing the operating system. But it is usable. For example, the shell command mv * otherdirectory would be written in Python as:

import os, glob
for fname in glob.glob('*'):
    os.rename(fname, 'otherdirectory')

Or more compactly:

import os, glob
[os.rename(fname, 'otherdirectory') for fname in glob.glob('*')]

Most tutorials for languages want you to understand the language first, and later how it interfaces with the operating system. For a shell, you need to understand how it interfaces with the operating system first.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.