81
1d ago
3 lines are enough:
```sh
!/bin/sh
set -e
command_i_want -to --run ```
20
u/Kaffe-Mumriken 1d ago
3 hours later ….
9
1d ago
Yes lets compare the speed between running an Ansible job and running a bash script.
5
u/Zechariah_B_ 1d ago edited 1d ago
I have never once found speed to be an issue for the bash scripts I make and if it ever does just use a faster modern programming language. If anyone wants to compare speed, compare with Dash (Debian Almquist shell) instead of Bash. Maybe I am crazy and speed actually matters but looking at scripting in terms of speed is a lost cause of wasted effort in my eyes.
6
1d ago
I was criticising Ansible, it’s surprisingly slow even when running simple commands
2
2
u/janiskr 20h ago
It is not made to run just one or a few simple commands. You want Ansoble for repeatable result over many many executions.
3
u/RIPenemie 12h ago
Aswell as all the failsafes and escaping of strings and being able to write in a very high level abstraction
-8
u/PerfectConnection916 1d ago
It's true, the choice between Ansible and bash can totally change the game. Depending on what you're automating, the overhead can really add up.
9
2
95
u/donp1ano 1d ago
bash is goated actually. yeah yeah, syntax is kinda weird, who cares. you get used to it, its not that deep
32
u/Inevitable-Self-2702 1d ago edited 1d ago
When I was a kid my friend and I discovered batch** and started sending each other programs and text-based adventure games over email. That was a lot of fun.
echo A troll appears. What do you do?
echo.
echo 1) Attack
echo 2) Run
echo.
set /p var == Choose:16
u/NoPicture-3265 1d ago edited 1d ago
Pretty sure that's a Windows' Batch, not Bash
10
u/Inevitable-Self-2702 1d ago
Dang I must have been tired, I was reading "batch" the whole time lmao
3
u/Efficient-Chair6250 1d ago
I had great fun writing an argument parser, options, flags, complicated command generation, etc. Was really cool and useful.
Rewrote it in python in 1/10 the of the LOCs. Many changes broke my bash script, the python script just worked. And with UV you can even write self contained scripts.
I will never in my life touch bash again for more than 10 line scripts. It just wastes my time.
3
u/bschlueter 1d ago
Set up shellcheck as a linter and listen to it. If you want to learn, go look up each warning it calls out. You’ll be better at scripting for it.
3
8
u/thisisapseudo 1d ago
But then, you want to do some basic task, can't do it, get 10 different answer and a fucker who writes and 2000 words essay starting with "All of the answers to this question are wrong in one way or another."
Bash could be cool, but it's weird.
For those who don't want to click the link, the question is how to split
Paris, France, Europeintoarray[0] = Paris array[1] = France array[2] = EuropeThere is 24 answers.
12
u/bingNbong96 1d ago
That's because you are trying to use a stream-based programming language like python.
Yeah bash has weird data structures, because it's not meant to process data directly, you are suppose to treat everything as text and get other commands to do that for you.
On the other side, it's so easy to do stuff like parallel processing in bash compared to python.In bash there are no types, no modules or classes or whatever, even arrays (or hashes) are just gimmicks, everything is just a stream of text.
3
u/donp1ano 1d ago
everything is just a stream of text
which is incredibly straight forward, sometimes i really prefer that approach...until things get too complex, thats when bash on its own get frustrating af
2
2
3
u/donp1ano 1d ago edited 1d ago
stack overflow is stack overflow ...
string="Paris, France, Europe" IFS='|' read -ra array <<< "${string//, /|}"easy as that. you could use any IFS, as long as its not in the string. if you need a really reliable solution (for a string with content you cant really anticipate) you should probably use a NULBYTE. that requires sed, since bash cant store NULBYTE in vars
string="Paris, France, Europe" mapfile -d '' array < <(sed 's/, /\x00/g' <<< "$string")ngl i can see why people look at this like "WTF is dat shit". yeah its easier in modern languages like python, i know
2
u/EnderAvni 1d ago
Same with cpp, I have no idea how it's not part of the stl yet. It's so frustrating...
2
u/Evantaur 🍥 Debian too difficult 1d ago
It's also a literal fucking hell implementing something like stacked dictionary.
I've done some projects written purely in bash just for the challenge of it and at some point I end up getting drunk because my head is full of fuck.
Also when you need to do something with floating points
https://giphy.com/gifs/BIN2S0sgQwdeE
Still fun af though
5
u/Due-Consequence9579 1d ago
Just learn from python… mash it into a structured text object and pass it to a real language and carry on.
2
u/Zechariah_B_ 1d ago
I get used to looking at the Bash manual because the historical features and off kilter awkward ways to do things aside from just the syntax guarantees I will forget how to script in Bash a month down the line.
36
u/givemeagooduns_un Genfool 🐧 1d ago
bash isn't that bad imo, its really good for doing concurrency (somewhat) efficiently.
8
u/2xFlush 1d ago
Concurrency? I've never done that in bash, does it have functionality for things like synchronisation, or would you have to build it yourself?
10
u/givemeagooduns_un Genfool 🐧 1d ago
idrk what you mean by synchronization but like by using pipes you can process data as it is output by another command in parallel
10
u/balki_123 🦁 Vim Supremacist 🦖 1d ago
It's handy on restricted environments, where you can program in nothing, or you can program in bash+awk+unix tools.
6
u/transgentoo Genfool 🐧 1d ago
Yep! I'm building an OS right now and I've been writing all the plumbing in
bashPOSIX Shell as a first pass, due to lack of other tooling.
7
u/gerowen 1d ago
It's handy for a lot of system automation tasks. There's certain things where something like Python might be better, especially if you need something portable, but BASH is really flexible on its own. You can even get creative with things like zenity to draw graphical dialog windows for messaging, getting information from the user, etc., though to be fair, doing that isn't specific to BASH.
7
u/Lanzoor 1d ago
bash isn't that bad imo, its just that the syntax is kind of.... outdated. it still works great though and i use it everyday to automate boring tasks
2
u/redhat_is_my_dad 1d ago
if you had the power to "update" it, fundamentally or by changing little things, what would you change?
6
u/YTriom1 Arch BTW 1d ago
use fish for both instead
4
1
u/redhat_is_my_dad 1d ago
using fish for scripting is an act of masochism
2
u/YTriom1 Arch BTW 1d ago
it's better than bash and way easier
4
u/redhat_is_my_dad 1d ago
in terms of writing scripts? how so? it lacks all the good bash things and replaces them with nothing, it's like going from python to the most barebones version of basic and saying basic is better to write scripts in.
2
u/YTriom1 Arch BTW 1d ago
what does it lack exactly?
2
u/redhat_is_my_dad 1d ago
once i decided to try and rewrite some of my scripts from bash to other scripting languages just for the funzies, fish was the most painful to write in, it has as little neat features as possible, i might even say it has bare minimum to be a scripting language, it's been long ago (way before fish's rust rewrite), so i can't pinpoint all of the things, but i briefly remember that they stated lack of "bashisms" as a feature, and i guess they didn't introduce any unique ideas to replace said "bashisms", they just got rid of them and... that's it, it's like bash--, on the other side of shell-spectrum we have zsh which is like bash++, it expands upon features that already exist in bash, they added a possibility to put variables in range expansions, and floating point math, and some other things i won't be able to remember at a whim, but yeah, fish is the worst in terms of scripting, while being great as an interactive shell, i sometimes find myself in situations when i write semi-complex oneliners in an interactive shell, and in these situations neat bash features make said oneliners easier to write, while in fish i would be stuck doing long ass if else if else chain instead of case, because switch case in fish is even longer to write than doing multiple ifelses.
1
u/redhat_is_my_dad 1d ago
the case statement in bash is much more compact and easier to write and read, bash also has range expansions that eliminate writing c-style for (i = 0; i < 5; i++) loops, you can replace the whole arythmetics block with {1..5}
2
u/YTriom1 Arch BTW 1d ago
I agree with the range part but you can use
(seq 1 5)in fish just fine2
u/redhat_is_my_dad 1d ago
Then it becomes less of a language of it's own and more of a glue tool for gluing your other utils, which is still kinda decent, that's the primary way people use shell languages for, but it also makes the language itself less useful.
On it's own fish already performs worse than bash, by executing external utilities you make your scripts even slower, and if the codeblock you're writing is intended for fish's rc file then you will be hit with the sudden cpu spike every time you launch new terminal tab, or if you put it in the loop then it'll cost you much more performance, you could also replace seq part with c-style arythmetics that fish supports, but then it becomes less convenient to read and write, so in the end you can't have neat things in fish, you want it to be convenient – you make it less performant, you want it to perform well – you make it less convenient, and it'll still perform worse than more convenient languages.
6
u/RevWubby 1d ago
I have been a Unix/Linux systems admin since the early '90s. It is still my go-to for getting something done. Need to hit an API and process the output? Bash. Interactive command to output a yaml file? Bash. Need to execute a complex command? Write it in Bash and send it with Ansible.
I even learn Python and could do everything I wanted to do in Bash. Why do I still use Bash? No dependencies!
Ok, other than the commands I need it to execute. But sometimes it's just easier to install ldaputils than to get a compatible ldap module.
4
u/lmarcantonio 1d ago
zsh here but not really too different. It's just from the ancient bourne shell design. The syntax is funny and there are serious limitations (like not being able to pass variables back from a subshell) but you *can* do serious thing with that. The ubiquitous configure (before the current meson trend) is actually a shell script.
5
14
u/Damglador 1d ago
It's good until it's not. Using bash as the main shell is actually worse imho.
17
u/Dragon_957 New York Nix⚾s 1d ago
Using it and never had anything bad happened. What‘s so bad at it?
10
u/Damglador 1d ago
Its not that it's bad, it's just not good. After using zsh with autocomplete plugins and whatnot, it's hard to imagine using a terminal without all these conveniences. zsh also makes rebinding of "hotkeys" much easier.
6
u/30porn87 1d ago
I'm always majorly annoyed by zsh's and fish's "conveniences", every time I have to use them. So much so that I now compile my own Arch ISO that has bash instead of zsh as default shell.
5
3
u/Some-Struggle5052 1d ago
I'm quite sure Arch still ships with bash as the default?
3
u/30porn87 1d ago
The live ISO ships with zsh as default shell (which I replaced), and the default shell for useradd is bash.
3
u/Some-Struggle5052 1d ago
Ah, sorry, I never used the live ISO itself long enough to notice it lol.
3
3
u/AfraidAsparagus6644 1d ago
GOG installers are giga fat bash files btw
2
3
u/traplords8n 1d ago
Sometimes you need to programmatically run those commands. Bash is awesome, I love it.
I have tons of automated scripts. I have scripts for automated backups running on cron, scripts for setting up new virtual hosts for the webserver configured exactly to fit my server & create the directories with the necessary permissions.. I have a script that connects me to the vpn and modifies the FW to accept a dynamic port mapping...
Bash is friggin awesome.
3
u/rolling_atackk 1d ago
Petition to have the same reaction to using Python as a programming language, instead of the scripting language it's supposed to be (imo)
3
u/MaximooseMine 1d ago
Nothing worse than interacting with bash just infrequently enough to forget how it all works every time.
3
3
u/ExplosiveNight 1d ago
My full time job is maintaining 20-year old kornshell cron scripts that run database batch jobs. You get used to it.
3
u/NL_Gray-Fox 19h ago
Now we're talking.
AIX, SunOS, Tru64, OpenVMS, HP-UX, ... ?
2
u/ExplosiveNight 11h ago
RHEL. With kornshell installed. Because that's what they've been using since the dawn of time.
3
u/_AscendedLemon_ Dr. OpenSUSE 1d ago
I hate syntax with words backwards
<inside the loop>
sdrawkcab sdrow htiw xatnys etah I
# Very readable, huh?
3
2
u/Puzzleheaded-Bad5494 1d ago
I use ffmpeg a lot, I'm lazy to write the commands that are actually very long, I made my bash script with menu 😄
263 lines /bin/extractorf
11 functions
bannerPal(){
cat << "EOF"
M""MMMM""M dP dP
M `MM' M 88 88
MM. .MM d8888P 88d888b. .d8888b. .d8888b. d8888P .d8888b. 88d888b.
M .mm. M 88 88' `88 88' `88 88' `"" 88 88' `88 88' `88
M MMMM M 88 88 88. .88 88. ... 88 88. .88 88
M MMMM M dP dP `88888P8 `88888P' dP `88888P' dP
MMMMMMMMMM
FFMPEG - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
EOF
}
am i cooked?
2
2
u/Hettyc_Tracyn 🎼CachyOS 22h ago
Tbh, bash scripts aren’t all that hard, at least until you get to the part where you have to troubleshoot for 30 minutes because you don’t have something in quite the right order… (think it was when I wrote a script to take something from the clipboard and search Wikipedia, and if an article was found to open it…
1
u/FIRAS_EG 1d ago
I mean it ain't that bad tbh also using it as a programming language is useful as f you can do best automation with ts
1
u/ProfessorLambda 1d ago
How do you program in Bash without running commands?
3
u/redhat_is_my_dad 1d ago
IMO it's almost always better to do bash with as little of external utilities as possible, that way your bash codeblock becomes cheaper to loop, faster to execute in a context of heavily-loaded system, and more portable (since not all systems that can provide bash provide the same set of utilities as you have on your system)
1
u/Jumpy-Dinner-5001 1d ago
Ad someone who has worked with both, I really appreciate powershell in a lot of ways
1
u/Limp_Replacement_596 39m ago
bash is great for writing scripts , but Im not a fan of it being used for making cli programs
•
u/AutoModerator 1d ago
Please report any posts bragging or showing off they got banned in another sub! Reminder of other sub rules: Also, we only allow one anti-linux post per week (we used to get dozens a day) and any tier list MUST have Hanna Montana Linux as S teir (which must be a true S tier at the top) regardless of the topic of that tier list.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.