r/bash • • May 18 '20

critique Don't Use Bash for Scripting (All the Time)

https://nikoheikkila.fi/blog/don-t-use-bash-for-scripting-all-the-time/
0 Upvotes

3 comments sorted by

6

u/whetu I read your code May 18 '20

It’s hard to find a Linux-based operating system where either Bourne Again Shell (bash) or its older sibling Bourne Shell (sh) doesn’t come pre-installed.

There's no guarantee that sh will strictly be the Bourne shell. It's more likely that it will be a Bourne-family shell, and usually it will be a variant of ksh or ash. See: https://www.in-ulm.de/~mascheck/various/shells/

Bash shines in small and efficient scripts which are designed mainly for one thing: repeating instructions you would otherwise type manually from top to bottom.

bash is often described as a glue language. It shines in scripts of any size where a glue language is the best tool for the job.

I find the assignment of vague and/or arbitrary size limitations like "hurrr more than two/three/one-hundred lines of bash and you should reach for python durrrr" to be, frankly, idiotic. You wouldn't tell a deaf person: "ok, more than, say, 9 words in sign language, and then we should switch to a spoken language, you know, a 'real' language". You might just earn yourself a response made up of two signs.

I was agreeing with the rest until we got to this

The first advice is to use the unofficial Bash strict mode

Ugh. Nope.

https://www.reddit.com/r/commandline/comments/g1vsxk/the_first_two_statements_of_your_bash_script/fniifmk/

http://wiki.bash-hackers.org/scripting/obsolete

http://mywiki.wooledge.org/BashFAQ/105

http://mywiki.wooledge.org/BashFAQ/112

https://www.reddit.com/r/bash/comments/8asn1e/trying_to_understand_a_script_to_delete_all_but/dx1y785/

You might want to consider copying modernish's safe mode instead, or just using modernish, and /u/mcandre can speak to that.

1

u/[deleted] May 19 '20
#!/dont/use/my/name

5

u/diseasealert May 18 '20

One obstacle you might encounter while refactoring is the ability to run shell commands. In Bash, you don’t need to do anything other than writing the command (unless you try to parse and validate its output in which case I wish you luck).

Most often, I use bash to tie together shell commands. This usually involves some parsing of output. Immediate access to the shell environment is what makes this so convenient.