Execute bash command from python shell console.

Execute bash command from python shell console.




Almost Every unix., Linux machine came with pre-installed python sometimes the distribution may came by an older version but you can update it by simpl command line.
python is a very powerful language that has a lot of features and an active community,
And the ability to download any library without mentioning its standard library (pre-installed library) that are installed by default exemple (

datetime, math, random, os, multiprocessing, subprocess, socket, email, json...)

   Désavantages of bash scripting


Python is a  Shell scripting language which means it is developed mainly to deal with the system and its files, just like bash.
Each scripting language has its benefits for example when it is came to dealing with systems files (copying, renaming, creating, reading...) bash provides better experience for every day using then any other language using cd, mv, cp, ls ... command.
Bash disadvantage appears in advanced usage such as scrolling files content or extracting specific information...
Also it is not useful for larger script and cannot deal with framework you will notice it's become more complicated and hard to understand.

Executing bash inside python shell


open python shell is very simple just type 'python' in your console and you should see this  >>> appear, now if you try to use command like cd, clear, mv, cp, ls... All what got is error telling you name 'ls' is not defined in this case we type in the beginning
>>> import os
To import os modules that came pre-installed in python which will allow as to use bash command inside python then
>>>os.system('ls')
Or
>>>os. system('clear') 

In general >>>os. system ('bash-command')
There are possibilities to make this command more comfortable and easy to use rather than >>>import os type
 >>>from os import system as foo
Then
>>>foo('ls') rather than >>>os.system('ls')
You can change the word foo with any word you want just make sure to use it thessame in the next line example
>>>from os import system as yourWord

>>>yourWord('ls')




Thank you for your attention

Related :
Cool package must have in termux

Comments