Bash Scripting

ovtback

New Member
Joined
Feb 11, 2020
Messages
5
Reaction score
3
Credits
35
Hello, I was wondering if anyone knows a good place to start bash scripting. I am just trying to learn it to be more flexible with the OS.

Thanks in advance, ovtback
 


The best way is to get some idea about what to script and then start scripting.
You'll learn by the way.

As for resource to get you quickly started here is good one, very concise and to the point, all you need to know on 1 page:
 
Hello, I was wondering if anyone knows a good place to start bash scripting. I am just trying to learn it to be more flexible with the OS.

Thanks in advance, ovtback
A traditional source for learning bash as a beginner is from the linux document project here:
It's old now, but still a useful resource to begin with.

Up-to-date docs which are also from a sound source are here, but a little more advanced:
This guide is also available as a pdf from a link on the site.

The canonical bash guide is probably this one:
It's by Chet Ramey, the current developer.

There are numerous resources online showing bash scripts from simple to complex so it's worth searching at times if one is looking for a script for a specific outcome. There's also the AI sites where one can describe in ordinary language what one wishes for a script to do, and it will usually write one. Then one ought to check the script thoroughly because AI can be rather cursory, inaccurate, or mistaken.

Basically, to learn bash scripting, one ought to write scripts and keep writing and amending them until they work as intended.
 
Last edited:
Me, I'm completely self-taught. It's probably why my scripting is 'untidy' & messy. Not the kinda thing I really want to share with the world!

Nah, it's nowhere near as bad as I like to make out.....but I've had no formal training, nor even followed any kind of generally recognised or 'semi-official' guide. In the course of researching summat else, I came across a tech blog article that was demo-ing some very simple examples.....so just for the hell of it, I tried 'em.

I have to say, I was intrigued by the possibilities that suddenly opened up!

I've always researched what I'm trying to do 'on-the-fly', as & when I need it. I'll copy examples, try 'em out. If they don't quite do what I want, having now got several years experience under my belt, I'll modify 'em, try 'em out again.....see what now happens. IF they then do what I want, they'll get incorporated into a longer script....

We build an awful lot of our own, home-brewed utilities for use in the 'Puppy' Linux community. In so doing, we tend to add GUI interfaces into them, especially to make them as simple as possible for 'noobs' and Windows 'refugees'.....and for these we either use the traditional gtk-dialog OR - my favourite - YAD (Yet Another Dialog), forked from the original Zenity project many years ago.

I find gtk-dialog - although it's considerably more powerful - to be an absolute PITA to code; it'll take several lines of code to define a single piece of the GUI, where YAD will often achieve it in a single short statement of just a few words.......because most of its functions are pre-defined, whereas with gtk-dialog, you start from a blank canvas & have to define EVERYTHING from the ground up.

But that's more 'advanced' stuff, for when you have some experience under your belt.

~~~~~~~~~~~~~~~~~~​

All I will say is this; have a 'play'. Find examples online, copy them into your terminal. Run them. See what they do. Experiment. Keep on trying stuff out; when you find stuff that does something you know you'll find useful, make a note of it. I've got simple text files full of 'notes' and 'jottings' that I've scribbled down over the years.

As you become more experienced, you'll find yourself referring to your 'notes' less and less; you begin to remember a lot of this stuff. You'll be surprised just how often you find yourself re-using the same constructions & snippets of code, over & over again.....just strung together in different ways, but.....never, EVER be afraid to research new & different ways of doing things. Bash is as complex and full of nuances as any other language you can learn.....and like any other language, it CAN take a lifetime to become fully and completely competent in it.....because - like any other language - it's constantly evolving.

I treat all this stuff as a hobby. I'm forever putting little scripts together just to automate small but repetitive tasks I'd done manually up until that point. Linux is fun like that; I dare say you COULD achieve the same outcomes under Windows, but the process wouldn't be anywhere near as enjoyable!

Remember; we're none of us 'expert' in Linux. We're ALL 'noobs' to a greater or lesser degree.....because there's always something new to learn. NEVER stop learning! And whatever else ya do, ALWAYS make sure to 'have fun' while you're at it....


Mike. ;)
 
Last edited:
I certainly don't want you to take this the wrong way. The man page for bash has been very helpful to me. I would highly suggest reading through it, or at least some parts of it. It is very informative. Aside from that, create a homework assignment. Think about something you want your script to do and then try to find a way to do it in bash. Reading the man page first is recommended so you get an idea of what bash is capable of.

Signed,

Matthew Campbell
 
@ovtback , we have an in-house resource available on this subject that might be worth taking a look.

If you take a look at our front page, you will see articles from our main Staff Writer, Jarret Buse.

euLMcEd.png


If you then go to our Search facility near top right and open it, search for

bash scripting

Posted by (start typing and it will autocomplete)

jarret

The output will contain a series from 1 to 9 on Bash Scripting.

HTH

Chris Turner
wizardfromoz

(surprised no one else suggested this)

BTW questions on scripting should be asked in Command Line, I will move this thread there.
 
The best way is to get some idea about what to script and then start scripting.
You'll learn by the way.

As for resource to get you quickly started here is good one, very concise and to the point, all you need to know on 1 page:
Thanks alot.
 
A traditional source for learning bash as a beginner is from the linux document project here:
It's old now, but still a useful resource to begin with.

Up-to-date docs which are also from a sound source are here, but a little more advanced:
This guide is also available as a pdf from a link on the site.

The canonical bash guide is probably this one:
It's by Chet Ramey, the current developer.

There are numerous resources online showing bash scripts from simple to complex so it's worth searching at times if one is looking for a script for a specific outcome. There's also the AI sites where one can describe in ordinary language what one wishes for a script to do, and it will usually write one. Then one ought to check the script thoroughly because AI can be rather cursory, inaccurate, or mistaken.

Basically, to learn bash scripting, one ought to write scripts and keep writing and amending them until they work as intended.
What are your thoughts on platforms like Codecademy? Are they a waste of money or somewhat worth learning these types of materials on?
 
Me, I'm completely self-taught. It's probably why my scripting is 'untidy' & messy. Not the kinda thing I really want to share with the world!

Nah, it's nowhere near as bad as I like to make out.....but I've had no formal training, nor even followed any kind of generally recognised or 'semi-official' guide. In the course of researching summat else, I came across a tech blog article that was demo-ing some very simple examples.....so just for the hell of it, I tried 'em.

I have to say, I was intrigued by the possibilities that suddenly opened up!

I've always researched what I'm trying to do 'on-the-fly', as & when I need it. I'll copy examples, try 'em out. If they don't quite do what I want, having now got several years experience under my belt, I'll modify 'em, try 'em out again.....see what now happens. IF they then do what I want, they'll get incorporated into a longer script....

We build an awful lot of our own, home-brewed utilities for use in the 'Puppy' Linux community. In so doing, we tend to add GUI interfaces into them, especially to make them as simple as possible for 'noobs' and Windows 'refugees'.....and for these we either use the traditional gtk-dialog OR - my favourite - YAD (Yet Another Dialog), forked from the original Zenity project many years ago.

I find gtk-dialog - although it's considerably more powerful - to be an absolute PITA to code; it'll take several lines of code to define a single piece of the GUI, where YAD will often achieve it in a single short statement of just a few words.......because most of its functions are pre-defined, whereas with gtk-dialog, you start from a blank canvas & have to define EVERYTHING from the ground up.

But that's more 'advanced' stuff, for when you have some experience under your belt.

~~~~~~~~~~~~~~~~~~​

All I will say is this; have a 'play'. Find examples online, copy them into your terminal. Run them. See what they do. Experiment. Keep on trying stuff out; when you find stuff that does something you know you'll find useful, make a note of it. I've got simple text files full of 'notes' and 'jottings' that I've scribbled down over the years.

As you become more experienced, you'll find yourself referring to your 'notes' less and less; you begin to remember a lot of this stuff. You'll be surprised just how often you find yourself re-using the same constructions & snippets of code, over & over again.....just strung together in different ways But never, EVER be afraid to research new & different ways of doing things. Bash is as complex and full of nuances as any other language you can learn.....and like any other language, it CAN take a lifetime to become fully and completely competent in it.....because - like any other language - it's constantly evolving.

I treat all this stuff as a hobby. I'm forever putting little scripts together just to automate small but repetitive tasks I'd done manually up until that point. Linux is fun like that; I dare say you COULD achieve the same outcomes under Windows, but the process wouldn't be anywhere near as enjoyable!

Remember; we're none of us 'expert' in Linux. We're ALL 'noobs' to a greater or lesser degree.....because there's always something new to learn. NEVER stop learning! And whatever else ya do, ALWAYS make sure to 'have fun' while you're at it....


Mike. ;)
Thanks a lot, really encouraging. Did you self learn everything computer related, or just specifically stuff like scripting?
 
I certainly don't want you to take this the wrong way. The man page for bash has been very helpful to me. I would highly suggest reading through it, or at least some parts of it. It is very informative. Aside from that, create a homework assignment. Think about something you want your script to do and then try to find a way to do it in bash. Reading the man page first is recommended so you get an idea of what bash is capable of.

Signed,

Matthew Campbell
I read a very tiny portion, but I will look through it thoroughly later on. Thank you.

@ovtback , we have an in-house resource available on this subject that might be worth taking a look.

If you take a look at our front page, you will see articles from our main Staff Writer, Jarret Buse.

euLMcEd.png


If you then go to our Search facility near top right and open it, search for

bash scripting

Posted by (start typing and it will autocomplete)

jarret

The output will contain a series from 1 to 9 on Bash Scripting.

HTH

Chris Turner
wizardfromoz

(surprised no one else suggested this)

BTW questions on scripting should be asked in Command Line, I will move this thread there.
Thank you as well for this, I will definitely check these forums out.
 
What are your thoughts on platforms like Codecademy? Are they a waste of money or somewhat worth learning these types of materials on?
Just had a brief look at Codeacademy, subscribed, but couldn't find a specifically bash course but there looks to be plenty on coding there. Can't say what they cover extensively without further investigation.

How you learn will in part be determined by a learning style. In my own case I prefer working through the text material such as in a book, or a pdf file which I know covers the material I'm interested in. Other people prefer to learn from videos, or a class environment, or from a mentor etc. Some people prefer to to pay to get what they regard as worth the money. Some people like only graded steps to move through material, but others are more adventurous. Some people are very disciplined, others more erratic. If one knows how they learn, they can apply that knowledge judiciously to their venture and thereby, perhaps, encounter fewer frustrations, but these things are not written in stone :)
 
Thanks a lot, really encouraging. Did you self learn everything computer related, or just specifically stuff like scripting?
Totally self-taught, right from the word go. I date back to the early days of the "home computer revolution" - late 70s/early 80s. Unlike many of today's younger GenZ/Millennial crowd, who've only ever known this stuff to always be there, I was growing up alongside the industry as the breakthroughs & 'milestone' developments actually happened, and have watched the tech world develop and evolve in ways that I never quite expected to see!

@Brickwizard is probably in the same boat as myself, except that I think he can remember even further back than I can.

(I did an after-hours computer course during my final year of secondary education here in the UK, and was utterly fascinated by the possibilities inherent in this new-fangled technology. Being naturally inquisitive, and having an "enquiring mind", it didn't take me long to get really "into" this stuff.....and having a passion for graphic design - a long-standing hobby of mine - along with the realisation that this was the coming way of doing things, I was experimenting with a lot of Adobe's early applications back in the days when they were still known as Macromedia).

It's one of those things. You're either technically-inclined, or you're not. I was.....and the rest, as they say, is "history".

~~~~~~~~~~~~~~~~~~~​

Me, I'll have a terminal and an IDE - my favourite is Geany, which is default in 'Puppy' anyway - open in one workspace, and a browser open in another. I play around; I experiment. I try stuff out all the time. If there's summat I don't understand, I'll switch to the browser and look up examples & stuff. It might seem a somewhat chaotic learning "style", but.......it works for me.

~~~~~~~~~~~~~~~~~~~​

Don't be afraid to ask questions if, as & when you need to know something. That's what we're here for; we like helping people, and we're NOT judgemental about WHAT OS/distro you run. At the end of the day, it's irrelevant.....and, as m'colleague @osprey says, we ALL learn in different ways. There is no 'set', 'fixed' method that works for everybody; over time, you develop your own way of doing stuff that works for you.


Mike. ;)
 
Last edited:
To get started in bash scripting, first make sure you're comfortable at the command line because a script is really just a bunch of shtuff that you might otherwise type at the command line. Then write your first "hello world" script to make sure you have the idea of the shebang and execute permissions. Scripting is programming, so if you're already familiar with pretty much any other programming language You'll have a big head start. So, let the fun begin...

Try to find someone else's shell script that's big enough to be non-trivial but not so big as to be too unwieldy (although, in the beginning, any non-trivial script might seem a bit unwieldy). It will help immensely if the sample script is one where you already know in broad terms what it does, especially if that functionality is something you care about. It will help even immenselier if there's some little detail or quirk or bug in how it works that you want to "fix" - having a goal or a purpose makes learning so much more "real" in my experience.

Go through that script line by line and section by section and figure out how it does its thing. Identify inputs and outputs, internal variable names, function names, control flows etc. Working with a copy, make small changes to try to achieve some goal and see how it works (or fails to work). man pages are your friend. This forum is a great resource, as is the arch wiki and the internet at large.

For example, while I'm "experienced" at shell scripting, I apparently still have much to learn. For me, a 650 line script isn't toooo unwieldy (for someone entirely new to scripting, a thirty line script might be more manageable - it's nice if you can see the whole thing on screen) so I picked up the main config script for my favorite Linux distro and took a look at it with an eye toward improving it. I achieved two things: 1) I learned some new techniques for shell scripting and 2) I learned that there's -nothing- in that script that would warrant changing it for any of my use cases.

If you're interested specifically in -bash- scripting, start with a bash script as your sample. If you're interested in shell scripting in general, maybe -don't- start with a bash script as your sample.
 
As with any kind of programming or scripting, make sure to make liberal use of comments. This can be very helpful later when you go back to look at your content to help remind you of what you were doing. In bash a # makes everything afterwards on the same line a comment. Comments are your friend.

Signed,

Matthew Campbell
 


Top