4 ms·
The only way to learn is by doing. Write yourself a bash script to automate some facet of your system. You will strike some sort of problem, that prevents you f
by simonblack 6y ago
The only way to learn is by doing. Write yourself a bash script to automate some facet of your system. You will strike some sort of problem, that prevents you from moving on until you can 'fix' that problem. In fixing that problem you will gain insight as to how bash works.
Ferinstance: loops - write a bash script to write your name 10 times. That's a nice simple challenge.
Then, once you can do that, move on to (say)regex with sed. Then move on to crontab scripts which have a different set of environment variables from a normal user's such that your 'normal' bash scripts might not act the way you would expect.
I just did a count of my scripts directory. There are just under a nice round 500 in there. Some scripts are probably shorter than the name I have given them, one is a monster at just under 15 kilobytes. That big one runs every night to back up my system sufficiently that I can revert my system to the backup-time last night within less than 2 hours, even if I have to do a complete reinstallation of the distro on my system's hard drive.
For what it's worth, my 'goto' place for my bash 'how-tos' whenever I find myself stuck on a bash problem is the stuff from that tldp website:
https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
https://tldp.org/LDP/abs/html/ https://tldp.org/LDP/abs/html/
- blobberdeen 6y agoaaah TLDP it is than, also that was pretty uplifting. Thank you so much for your time, guess one just cant go away from documentations.
- simonblack 6y agoI treat those two web pages at TLDP as reference material. There is nothing more boring and sleep-inducing than trying to read dry, factual information when you have no underlying reason to do so, as you have so rightly pointed out. That's why you give yourself some sort of project. Any project! It's the reason to use the reference material when you hit a problem you don't know how to fix. There's a reason for that old saying "Build one to throw away". By the time you finish your project, you will have learned so much that you will want to throw away that ugly first attempt and write it properly the next time.
- blobberdeen 6y agoabsolutely, also refs for bash on TLDP are for bash 3.x.x, will it still be good for 5.x.x ?
- 5c0tt-b0t 6y agoShells like bash/ash/dash etc are not updated often syntactically, so refs for 3.x.x will still be great reference. I totally agree with @simonblack 's great answer, its the "running into a problem and fixing it" which is the learning process (especially with shell scripting, that doesnt come with a fancy built in doc system, that modern languages have, but remember its shell, its not a modern language) you will find many "gotcha's!" along the way, remember them and not make that mistake again. As simon said, start small, work up, scripts should all be tasks, find some useful tasks you want automated, I have some that backup my PC and mobile devices, one to sync my music between all of them, one that can disable/enable apps on my phone/tablet. Someone who showed me the way once suggested my first script should be a PS1 prompt, till this day it was the best first project and one I'll suggest to anyone getting started as its fast, simple, fullfilling but you find many problems along the way I assure you! Start by making a prompt in format you like, then add some colour to it, make it multiline, add git status to it, write two functions "git_branch" and "git_dirty" and add these to your prompt command, add exit status indicating whether last command failed, make the symbol change if you are root, or make parts change colour instead, depending if you are root or command fails. I actually made a bash cheatsheet for reference when I'm scripting so that I dont have to search the web, it contains hyperlinks that take you to tldp which is the site suggested by Simon, the very bottom of the sheet has links to all the best refs you'll find on the net, thus answering your question, the markdown version is finished, I still need to convert it to shell script, pdf & html (I'll do it one day!), hope it helps: https://github.com/5c0tt-b0t/basheet https://github.com/5c0tt-b0t/basheet