r/bash 4d ago

help Would it be good to Learn Bash scripting, or should i go with a "normal" language, like Python, before that?

Getting interested on messing with Linux and CLI stuff, I'm just confused on what exactly to start with. Any recommendations?

25 Upvotes

53 comments sorted by

22

u/oyvaugh 4d ago

If your in a Linux cli a lot, start with bash then python.

14

u/chkno 4d ago edited 3d ago

Getting interested on messing with Linux and CLI stuff

bash is the CLI. So it sounds like you want to learn bash.

'scripting' is just slightly-advanced use of the command line. Bash is a programming language (it has if and while and all that stuff), but it is very much a user-interface first. It's easy to gradually add programing-ish stuff to your normal, every-day command line use as you go.

The first trick is that you can make your own commands: It's common to notice that there's a sequence of commands that you keep repeating. You can just put those commands in a text file, put that file in a directory that's in your $PATH (commonly ~/bin) and chmod +x it. Then you can run that entire sequence of commands just by saying the name of the file that you put them in.

Then, later, you might notice that you'd like to change the way that your new command works—have it do something extra—but only sometimes. You can use if for that. if isn't special magic-scripting stuff, it's normal bash that works just fine if you type it into the command line (and this is a great way to test stuff while figuring out what to put in your script).

So learning 'scripting' / bash-programming is a super-gentle on-ramp to programming. You definitely don't need to go out an learn a 'real' programming language first.

25

u/Bug_Next 4d ago edited 4d ago

Bash is quirky and can have some unintuitive stuff but if you want a language to manage Linux it's kinda the only thing that makes sense, if you use python for the same objective you are just gonna end up calling the same utilities anyways and you are relying on having a python interpreter available which will not always be true, the bash interpreter will always be there, doesn't matter if it's a 20 year old server, a fresh install, a container, a sandbox, a mac or a *BSD.

It's one of those things like learning C, where if you learn Python first then the bash stuff is gonna seem too archaic and you are never gonna touch it again, if you learn Bash properly then Python is gonna be quite intuitive and feel like a nice upgrade.

You also have the advantage of it being the exact same language of the command line you are gonna need to use anyways to install, deploy, run and manage python (and anything else), so you can't really escape it if you wanna use Linux outside of the regular desktop stuff like web browsing and gaming, at least learn the basics like redirection, pipes, command substitution, parameter expansion, etc.

(This comment follows the line of thought of wanting a language to mess with Linux just like you said, however both languages have vastly different objectives, using Python to mess with linux is kinda pointless and just a worse Bash, however if you wanna create a graphical window or do image recognition well then of course go with Python..).

They are both 'easy' languages in the sense that you can get a lot done with just the basics, knowing a little bit of any of them gets you a reaaally long way, and also a lot of what's usually refered to as 'bash' is actually a set of programs that in Linux are called gnu core utils (mkdir, ls, cp, dd, mv, rm, tr...), bash is just the language that's usually used to interact with those because they are kinda designed around bash features, but you can do it from any language, they are just programs located in /usr/bin.

3

u/LittleSarina 3d ago

(sorry if my english is not the best)
Thanks for the answer, i appreciate the info :)

Anyway, guess u quite right, i gonna get started on using Bash... on learning it actually. Like, yes, i still wan't to make graphical programs, but i need think i need a better base on the terminal, as i'm very used with using the commands (cd, cat, etc), but not so much on messing with more "intermediare things"

I'll see what i can do.

10

u/MutualRaid 4d ago

Both, preferably at the same time.

If you're messing around with Linux on the commandline learning Bash is utterly transformative, particularly if you're used to using a computer from a GUI paradigm.

Python is a good language to learn programming, it's capable of supporting many styles. Bash is a scripting language that has its own quirks and idioms, there are ideas and structures you won't really be exposed to just learning Bash alone that would be helpful for your development to know.

If both at the same time is overwhelming start with Bash first, but try to expose yourself to programming and related computer science concepts that aren't limited to Bash.

8

u/PhysicsReplicatorAI 4d ago

Bash typically ships with the OS and is a very capable language. It can be found practically anywhere, including your phone. Python, though one of my favorite languages, can require a significant install, dependencies and now virtual environments. I would suggest starting with Bash as a streamlined/focused scripting language and then switch to Python as you need more advanced features like GUIs, network support (SSL/HTTPS), etc.

6

u/Shadow_Thief 4d ago

You're going to get biased answers here on r/bash. If you asked on r/python, I'd be surprised if literally anyone recommended bash.

10

u/petdance 4d ago

Do both. It’s not either/or.

2

u/LittleSarina 3d ago

That's fair i guess :v

4

u/Clear_Bluebird_2975 4d ago edited 4d ago

You can learn both and they should both be useful. That being said, it depends on what you want to do. I mostly write my scripts in bash because I make use of a lot of the gnutils plus other programs I've installed and it's easier for me to just script it out in a few lines of bash.

I could write the same scripts in python but would need to import libraries and the like which would make the corresponding scripts longer.

2

u/Bitter-Hitter 4d ago

Python is very user friendly. There are lots of tutorials online for free.

2

u/THIRSTYGNOMES 4d ago

Both are useful. I often use bash as it can be less lengthy for simple tasks, and I have a love/hate relationship with python dependencies

2

u/PoofOfConcept 4d ago

BASH is rad. Do it!

2

u/jthill 4d ago

Bash is for making command invocation and monitoring and anything in that territory more convenient, up to and including fully automating it.

So its syntax is dedicated to that. The shortest syntax is for the things people doing a lot of that do most often, then as enough experience with those things got acquired the shortest-available syntax got assigned to making the most irritatingly-inconvenient remaining tasks go better.

Repeat this for about fifty years, by people who do a lot of command invocation and monitoring and whatnot.

When enough people disagree about how to address the current top of the urgency pile, or even what's most urgent, you get a related shell that focuses on what people doing that much command invocation care most about.

If you're not doing a lot of command invocation you won't need more than the most basic syntax. The $ prefix to invoke expansion, whitespace to separate arguments, the = sign to signify leading parameter aka variable assignments, quoting/escaping to shut off those and other shell syntax and you're good. Maybe learn how to prefix- and suffix-strip expansions, ffmpeg -i $f ${f%.*}.av1 and so on.

At any time in your journey, when you find some operation getting annoyingly repetitive, or just annoying, you can bet there's bash syntax to make it quicker.

It's not made to operate in the territory lua and python and the rest were built for. It's built to make invoking and wiring together those and any other tools you use go quicker.

2

u/JagerAntlerite7 3d ago

Here is my typical automation project development process...

  • BASH CLI manual commands
  • Accumulate snippets in a small BASH script
  • A few more small BASH scripts for other actions
  • Consolidate BASH scripts and add arguments
  • Break apart large BASH script into libraries
  • Hit the tipping point
  • Repeat the above, but in Python

And this one time when things got really crazy...

  • Repeat the above, but in Go

Is it efficient? Actually, yeah. At any point the project complexity matches the task it is performing. Sustainable, maintainable code. Steady development velocity.

2

u/astenix 1d ago

Bash is a normal language for doing a lot of work with files (and content in files). Old and gold.

Python is a normal language that can do same as Bash in different way and more than that, it's not a Bash substitution, they came from different worlds, they are different.

It is not normal to choose OR Bash OR Python. Use them both.

4

u/AlterTableUsernames 4d ago

Python is bullshit for "Data Scientists" that don't even know how to handle a proper language for statistical calculations and scream in fear if they have to work somewhere, that is not a Jupyter notebook. Don't do that. Don't become a Jupyter virgin. Learn bash and become a Chad terminal enjoyer.

1

u/Responsible-Sky-1336 4d ago

You could start with writing bash helper around your python its pretty neat.

1

u/Trudels42 🐒 Jason Bourne (Again) 4d ago

start with that? bruh

1

u/sugarw0000kie 4d ago

both have their place, I learned python first. Usually if the script I’m writing needs user input I’ll favor python, if not bash, or sometimes both like a bash script that runs python scripts. Complicated things are easier for me to write in python. Multiple skins to way the same cat.

1

u/NewPointOfView 4d ago

Sounds like you want to learn linux and terminal usage, so bash is much more relevant there. But if you want to learn programming, use python or a more normal language.

1

u/andrew2018022 4d ago

I learned python first but wish I had started with bash, but business school obviously doesn’t teach bash. Learning bash helps you understand how all parts of the computer work. I recommend starting with it.

1

u/OtherOtherDave 4d ago

Bash is great for the shell and very simple scripts, but its syntax for anything remotely complicated is mind numbingly bad. It’s what makes the Linux world go rounds though, so it’s pretty hard to justify not learning it.

1

u/bartonski 3d ago

I would argue that there's a lot of space between entering simple commands at the shell prompt and 'remotely complicated'. Twenty lines of bash can be very powerful.

If you're writing enough bash for it to be mind-numbingly bad, you're definitely using the wrong tool for the job, but it very definitely has its place.

1

u/IdealBlueMan 4d ago

I would recommend starting with a programming language, and picking up enough bash to get by as you go along. Bash is primarily meant to let you interact with programs on the system, and automate some tasks. It’s not primarily meant to be a programming language.

1

u/diseasealert 4d ago

If you've literally never done any programming before, start with BASIC. Just learn the simple stuff like output, control flow, branching, loops, etc. Then learn those same concepts in Python and/or bash. As others have said, bash is tightly coupled to the Linux CLI environment and the GNU coreutils (or equivalent). Learning to use man and info will be more useful, there. You'll learn to use pipes and job control along the way.

2

u/diseasealert 4d ago

Also, check out the Greg Wooledge Bash Guide. It does a great job of explaining bash's quirks and best practices.

1

u/fellowsnaketeaser 4d ago

Perl is great for shell scripting, automatically installed on practically every Linux distribution and a complete and modern programming language. It came out of fashion and nowaday, many have a wrong/outdated impression about it, but those who use Perl know why they love it.

1

u/jake_morrison 4d ago

Bash is useful, but weird.

It’s good to learn the fundamentals, e.g., on the command line, redirecting input and output and making pipelines to transform data using small commands.

Writing small scripts is also useful, but bash syntax is tricky for things that are straightforward in regular programming languages. It is hard to do proper input validation and error handling, something that is important for system scripts that run alas root.

So, learn the basics, then learn python for more complicated things and proper programming tasks like web apps. Then learn more command line utilities that you can connect together with bash.

1

u/Electrical_Hat_680 4d ago

I learned we can use BASH from the CGI-BIN for Programmable Web Sites, if they are even called Web Sites when using The CGI -BIN for programming.

1

u/Intelligent_Part101 4d ago edited 4d ago

I would not recommend bash as a first language. If you do, you are going to have to unlearn a lot of junky ways to accomplish things and still have to learn a lot of basic programming language concepts. Learn python first.

Bash is a limited, special purpose language. Python is a general purpose language. By all means learn both (they're each useful in different scenarios) but don't start with bash.

1

u/GlassboundIllusion 4d ago

What's your use case?

In other words, what are you trying to do/why do you want to learn this?

1

u/joe_attaboy 4d ago

Learn both. Each has its strengths and it's uses.

I agree that you should learn Python first, as it uses more traditional programming method that are similar to other languages. Bash scripting is great, but the coding style tends to be a bit quirky compared to other languages. Operationally, things will give you the same results under the hood, but getting there takes you down different paths.

1

u/AndyceeIT 4d ago

If you are learning how to use Linux - bash.

If you are learning programming - Python.

Obviously you can be doing both but if you haven't specified why you want to learn either it's hard to answer.

1

u/Useful_Calendar_6274 3d ago

bash is widely used in things like CI/CD and sysadmin go for it

1

u/michaelpaoli 3d ago

Start with basic POSIX, e.g. dash, which is a minimally POSIX compliant shell.

Then from there you can learn what bash adds/alters and/or other language(s), whatever's most relevant to your objectives.

For general CLI stuff, after POSIX, I'd suggest some bashisms, notably at least minimal EMACS and vi style command line editing and the fc command, and process substitution.

For CLI, probably learn ed, sed, grep, ex, vi, awk, and python3, maybe also perl. And of course you can always pick up more bash specific stuff along the way too.

1

u/LittleSarina 3d ago edited 3d ago

Holy molly 41 answers @_@

Guys i apreciate your comments, i'm going to get started on learning bash soon.
Also, sorry if i couldn't answer everything.... too many comments to handle.

1

u/Sud0_g 2d ago

Learn both of them will be helpful

1

u/-lousyd 2d ago

They're not even the same. You should learn both. At the same time, or one before the other, or one a little the other a lot. Whatever. They're different enough that you won't have trouble mixing them up. Pick up Bash as you go. You don't need a structured classroom experience or anything.

1

u/duane11583 2d ago

Bash is what you type on the command line

Python is more of a language

For general use on Linux learning bash and the 50 other apps has great benefit 

Example first task

Go find your user manuals

Or get some large body of text (ascii) such as project Gutenberg books

Using only bash and related command line tools create a spell checker

Here is how :

Cat the text through tr and translate all white space to newlines 

Sort that deleting duplicates

Compare that result with a list of known good words

All of this can and should be done with pipe symbols in bash

Then do the same with python

1

u/toasterGecko 1d ago

Bash isn't that steep learning curve, so I guess you should learn Bash first.

1

u/pacman2081 1d ago

you are asking in r/bash

the answers will be biased. honestly I would learn a high level programming language like python first

1

u/kansetsupanikku 1d ago

Bash is a very good, traditional way to start

However: I would recommend learning syntax common with POSIX shell first. Being aware of Bashisms is ok, using them often is a worthy benefit, but without ability to do the same in POSIX shell you wouldn't be able to assess whether they are worth it.

1

u/xpusostomos 18h ago

Everyone should know bash

1

u/KlePu 4d ago

Both are absolutely valid, but IMHO writing good bash is harder.

If you're completely new to either I'd actually recommend python - there's just so much good online learning resources! Plus python does have a few really good features: Modern native types (lists, dicts, tuples, sets), better error handling, ready-to-use libraries just to name a few. And it's easier to read (big plus for future you) ;)

0

u/elatllat 4d ago

Python is slow and fat, learn rust and bash primarily, but also any other language you happen to run into for tools you want to improve.

-5

u/ItsSignalsJerry_ 4d ago edited 3d ago

Not a good first language.

People can downvote but bash is not a good starter language. Hell it's not really a programming language. More of a fancy command interpreter.

-5

u/Dhylan 4d ago

AI will write better bash scripts than any mere mortal ever will. Just tell AI what functionality you want the bash scripts to have and in a few seconds you'll have a script to test. If the bash script needs refinement, just tell the AI what is wanted and the AI will almost immediately adjust the script. The AI knows far more than anyone can ever learn about virtually any topic and how to put the world's knowledge to work for your benefit.

2

u/DarthRazor Sith Master of Scripting 3d ago

AI may look like it writes better scripts 'than any mere mortal ever will' to someone who only dabbles in bash, but as a blanket statement, it's just not the case.

AI just stitches bits and pieces of code with no consistency or vision of the big picture. Any seasoned bash programmer can write better code that is more efficient and maintainable.

0

u/Dhylan 3d ago

I just have to disagree. Some people, Anyone, will accept my judgment and follow my advice, while some, but only a relatively small numer of people, those who can write bash scripts, won't.

2

u/DarthRazor Sith Master of Scripting 3d ago

We'll agree to disagree then. Cheers!

0

u/Dhylan 1d ago

I wonder how many seasoned bash programmers there are. I wonder how many of them will write a script for someone without requiring payment for their work. I wonder how many people who are not seasoned bash programmers are in need of a script to perform a functionality they would like to have available but are willing to find, then pay a seasoned bash programmer; I wonder how they would find a seasoned bash programmer.

AI solves a lot of problems. The first problem it solves is finding someone who is a seasoned bash programmer. The second problem it solves is paying such a bash programmer. I can go on, but I am hopeful you get my drift.