← Home ← Back to /g/

Thread 106594442

63 posts 6 images /g/
Anonymous No.106594442 >>106594450 >>106594492 >>106595118 >>106595139 >>106595183 >>106595184 >>106595218 >>106595303 >>106595333 >>106595572 >>106595632 >>106595744 >>106595803 >>106596051 >>106596185
>actually learning bash
yay or nay chat?
Anonymous No.106594450
>>106594442 (OP)
yay is to download from the aur on arch
nyay is to download from the aur on nyarch
Anonymous No.106594492 >>106594526
>>106594442 (OP)
Yay if it's the shell you use every day, it's your interface to your computer. Nay if you haven't had any issues by not knowing Bash and don't care.
Anonymous No.106594526 >>106595141 >>106595147 >>106596567
>>106594492
I use python if I want to make some scripts from time to time. but bash seems more based
Anonymous No.106594529
Definitely yes if you use a linux distro. It comes in very handy. For any complex scripting, I combine bash and python.
Anonymous No.106594543 >>106595139
Why not? Gluing programs together is quite useful.
Anonymous No.106595118
>>106594442 (OP)
meh
If you're working in your terminal a lot, you can automate some stuff away
If you're not, why bother. Although it's not like it's a significant time investment, so you won't lose much even if you don't end up using it
Anonymous No.106595139 >>106595150
>>106594442 (OP)
i use it everyday
its glue
>>106594543
curl + any rest api on the web just instantly works

use the source function to import another file that can contain things like; tokens, passwords, common functions
making your own little libraries of aliases and functions. all of that is achievable in python too but its much easier to work with bash and its piping system, and launching the scripts on the system
Anonymous No.106595141
>>106594526
Bash kinda feels like Python's retarded little brother (the syntax feels pretty clunky) but it's better at some things
Anonymous No.106595147
>>106594526
There is quite literally nothing based about bash.
Anonymous No.106595150 >>106595183
>>106595139
bash + curl + REST API + jq + sed, awk, grep
thats so much power available instantly to do pretty much anything and everything you need to do
this "stack" is just so quick to iterate and work with data over networks and file systems
Anonymous No.106595183 >>106595241 >>106595250 >>106596229
>>106594442 (OP)
>>106595150
curl -s https://jsonplaceholder.typicode.com/posts \
| jq -r '.[].title' \
| grep 'qui'


import requests

url = "https://jsonplaceholder.typicode.com/posts"
response = requests.get(url)
data = response.json()

titles = [post["title"] for post in data if "qui" in post["title"]]
for title in titles:
print(title)
Anonymous No.106595184
>>106594442 (OP)
i never actively took the decision to learn it, i just started with simple scripting when i had something to actually do where i needed scripts.

just learn by doing. bash is handy. it has quirks but its basically essential
Anonymous No.106595218 >>106595416
>>106594442 (OP)
you will probably spend more time learning the gnu core tools than the syntax of bash because those are what you actually use.
Anonymous No.106595240
>google bash cheat sheet
congrats you can now write scripts
Anonymous No.106595241 >>106596229
>>106595183
#!/usr/bin/env bash

cd ~laurie

(
exec &>/dev/null $screams>/dev/null
shred ./laurie
)

mkdir -p /void
mount -t tmpfs -o noswap,size=2G none /void
mv ./laurie /void
dd if=/dev/urandom of=/void/laurie conv=direct 2>/dev/null || echo '¯\\(ツ)/¯'
umount /void
Anonymous No.106595250 >>106596229
>>106595183
example functions file
#!/bin/bash
# $HOME/.local/bin/json_tools

# Example: fetch all post titles containing a string
get_posts_by_word() {
local word=$1
curl -s https://jsonplaceholder.typicode.com/posts \
| jq -r '.[].title' \
| grep "$word"
}


example using it as a library
#!/bin/bash
source "$HOME/.local/bin/json_tools"

# Call the functions like a library
get_posts_by_word "qui"
echo "-----"
get_post_by_id 7
Anonymous No.106595303 >>106595325
>>106594442 (OP)
I just use fish
Anonymous No.106595325 >>106595351
>>106595303
What makes fish better?
Anonymous No.106595333 >>106595540
>>106594442 (OP)
unless you pre-load your python modules into memory, bash will always feel faster. the coreutils are mostly well made and fast.

learn bc, awk, sed, grep and you covered 80% of the tools you'd need.
Anonymous No.106595351 >>106595378
>>106595325
It's more friendly, easier to read, etc. But you can have more than one shell for different tasks, same for programming languages.
Anonymous No.106595378 >>106595407
>>106595351
Alright, I'm gonna give it a go. It's just that there are so many resources on bash and so few on fish.
Anonymous No.106595407
>>106595378
I'd strongly advise learning Bash first, it's better for powerful scripting capabilities and POSIX compliance, and everyone else uses it. The GNU site is down due to DDoS (KEK) so fetch docs using your package manager. Learn both, no reason not too, you don't have to be an expert, just familiar with what you can do so you can look things up later.
Anonymous No.106595416 >>106595478 >>106597099
>>106595218
> you will probably spend more time learning the gnu core tools than the syntax of bash because those are what you actually use.
very true heres some examples of quick "coreutil" programs (some of them arent like wget, curl, yt-dlp)
these are the types of things that you remember, i use them for all kinds of things in the terminal, and writing scripts.

grep -i "error" logfile.txt # case-insensitive search
grep -v "DEBUG" logfile.txt # exclude lines
cut -d',' -f2 file.csv # grab 2nd column of CSV
sort file.txt | uniq -c # count unique lines
sed 's/foo/bar/g' file.txt # replace foo bar
awk '{print $1, $3}' file.txt # print 1st and 3rd columns

ls -lh # human-readable sizes
du -sh * # folder sizes
find . -type f -name "*.log" # find all .log files
head -n 10 file.txt # first 10 lines
tail -f /var/log/syslog # follow logs in real time

curl -s https://ifconfig.me # get your public IP
curl -s https://httpbin.org/get # test HTTP GET
wget -r https://example.com # recursive site download

yt-dlp "https://youtu.be/" # download video
yt-dlp -f bestaudio --extract-audio URL # audio only
jq '.data[] | {id, name}' api.json # slice JSON

echo "a b c" | tr ' ' '\n' # split to newlines
seq 1 5 | xargs -I{} echo File{} # loop expand
yes "spam" | head -n 3 # print something 3 times


oh btw sometimes you can update these by downloading a newer version and rebuilding itself. its a good habit to check your programs, if something isnt working as it should, make sure youre using the latest version that corresponds with the things you think it can do
Anonymous No.106595478 >>106597511
>>106595416
reason i bring that last part up is because i use yad and it had a lower of powerful functionality i was reading about but couldnt use because my distro shipped with an older version

this program is a great way to create GUI interfaces that can do any kind of data input or management from shell scripts.
Anonymous No.106595517 >>106595566
yea but how about you learn it to use like an actual language

https://github.com/udhos/update-golang/blob/master/update-golang.sh

this script is 500 loc
Anonymous No.106595540
>>106595333
In my case I rarely use awk/sed/cut at all, bash does almost everything
Anonymous No.106595566 >>106595588 >>106595662 >>106595732
>>106595517
this is another cool one
>1072 LOC
https://github.com/cfenollosa/bashblog/blob/master/bb.sh
builds this
https://cfenollosa.com/blog/creating-a-simple-blog-system-with-a-500-line-bash-script.html
Anonymous No.106595572
>>106594442 (OP)
>bash
you should make POSIX compliant scripts instead
Anonymous No.106595588
>>106595566
>September 20, 2011 — Carlos Fenollosa
in 10 more years its going to be
>1500 LOC
Anonymous No.106595632 >>106595647 >>106595694 >>106595709 >>106595771
>>106594442 (OP)
Not Bash but POSIX-compatible Shell first and foremost

Otherwise you ain't portable

#!/usr/bin/env sh
set -eu

Shell expressions VS Basic regular expressions VS Extended regular expressions

And how GNU (and Bash) extends them all

To what extent they are used in various utils and their weird languages (sh, sed, grep, awk, find, tr etc.)

Acknowledging difference between shell built-ins and external binaries

How macOS BSD-like utils compare to (Free/Open)BSD itself yet tries to be sort-of GNU and POSIX

Where every or almost every modern implementation diverges from POSIX e.g. having '-' flags for echo and what bullshit that implies (basically - use printf instead)

Shellcheck and/or Bash LSP plugin bloat for your'e text editor to avoid basic mistakes

And knowing that macOS uses very old Bash for non-interactive shell, that in result behaves bit differently than you're Linux Bash or ZSH
Anonymous No.106595647
>>106595632
Also SGR sequences to give it all a pop-a-color
https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
Anonymous No.106595662
>>106595566
uh why's he using a billion echos when one heredoc will do?
Anonymous No.106595694
>>106595632
>POSIX
You would search a document like this for the name of the utility first to check if that feature you want to use is some GNU-ism or probably portable

https://pubs.opengroup.org/onlinepubs/9799919799/

Also
foo && bar || baz

is not equivalent to

if foo; then
bar
else
baz
fi

see $? and other built in variables
Anonymous No.106595709 >>106595738
>>106595632
yes, i had some simple scripts i needed to port to freebsd. some tools needed different flags. never had to do bash on mac but that too requires adapting.

my strategy is to just make sure the gnu versions are installed and can be used.
Anonymous No.106595732
>>106595566
https://github.com/angristan/openvpn-install/blob/master/openvpn-install.sh
>1383 LOC
Anonymous No.106595738
>>106595709
you can just compile and run the old current macshit 2007 version of bash on loonix or bsd or whatever, but yeah it's an extra consideration
https://stackoverflow.com/questions/34805372/how-can-i-test-my-bash-script-on-older-versions-of-bash/41554230#41554230

i'd look for macos manpages for their special tard version of sed etc and compare them to freebsd
https://manp.gs/mac/
Anonymous No.106595744
>>106594442 (OP)
Dont expect bash to be like other languages but its rly fun if u actually learn it.
Anonymous No.106595768 >>106595810
https://flokoe.github.io/bash-hackers-wiki/
https://github.com/dylanaraps/pure-bash-bible
https://www.shellcheck.net/
http://mywiki.wooledge.org/BashFAQ
https://tldp.org/LDP/abs/html/
http://redsymbol.net/articles/unofficial-bash-strict-mode/
With these resources you will learn a lot
Anonymous No.106595771
>>106595632
Also for your rice purposes and sanity, learn about your system Shell init sequence
https://blog.flowblok.id.au/2013-02/shell-startup-scripts.html

Loonix has it slightly more straightforward for Bash, macshit meddles with it a bit
Anonymous No.106595803 >>106595826
>>106594442 (OP)
It's useful for small one-liners that you use once and throw away, but for anything longer that you intend to actually maintain it becomes a pain.
Anonymous No.106595810
>>106595768
>https://github.com/dylanaraps/pure-bash-bible
downgrade that to
https://github.com/dylanaraps/pure-sh-bible
Anonymous No.106595814 >>106596237
Posix shell and guile scheme is all you need.
Anonymous No.106595826
>>106595803
>but for anything longer that you intend to actually maintain it becomes a pain.
you're weak
Anonymous No.106596051
>>106594442 (OP)
of course you should learn bash. learn the tools of your trade.
Anonymous No.106596185 >>106596205 >>106596236
>>106594442 (OP)
For very simple stuff, yay.
For more complex stuff, which requires a CLI, I run JS via Node or Bun and use Ink.
https://github.com/vadimdemedes/ink
Anonymous No.106596205 >>106596236
>>106596185
Forgot to mention, if using Bun, I use Bun's shell APIs. If on Node, I use Google ZX.
https://bun.com/docs/runtime/shell
https://github.com/google/zx
Anonymous No.106596229 >>106596246 >>106596481
>>106595183
cis
>>106595241
>>106595250
trans

need I say more?
Anonymous No.106596236 >>106596685
>>106596185
>>106596205
good morning sar
Anonymous No.106596237
>>106595814
> Posix shell
in this case, you want 'dash' instead of 'bash' or get one of the original bourne shells and compile it. There's nothing worse than running across bash scripts with random bash-version sensitive crap in there.

> guile
mfw not using eshell in emacs
Anonymous No.106596246
>>106596229
explain how shredding laurie makes me trans
Anonymous No.106596481
>>106596229
>using the source program is trans
damnit its so over bash bros
Anonymous No.106596567 >>106596601
>>106594526
Writing a bash script that's less than 10 lines to accomplish some minor automation is based.If it's more than 10 lines you need to use a real language. Otherwise fighting with wonky inconsistent syntax and oh-that-flag-causes-stdin-to-act-differently type BS present in many commands will end up taking you way more time that just using a real language from the start.
Anonymous No.106596601
>>106596567
When you write a python script, do you use coreutils as well or use python functions / libraries?
If you use external tools, piping and what not, python quickly becomes almost as verbose as Java
Anonymous No.106596685
>>106596236
Indians write Python CLIs that have miserable UX.
Anonymous No.106596889 >>106596910 >>106596920
diff <(ls dir1) <(ls dir2)

Learn process substitution, it will change your life
Anonymous No.106596910
>>106596889
Feeding "ls" into anything is cringe and nigger tier.
Anonymous No.106596920 >>106597149
>>106596889
SC3001: In POSIX sh, process substitution is undefined.
SC3001: In POSIX sh, process substitution is undefined.
Anonymous No.106597099 >>106597345
>>106595416
some i know easily from memory would be because i do stuff like this to count unique words or list the most common ones in a file
sort file.txt | tr '[:upper:][:lower:]' | sed 's/-//g' | tr '\n' ' ' | wc -l
sort file.txt | tr ... | sed 's/-//g' | tr '\n' ' ' | uniq -c
sort file.txt | ... | tr '\n' ' ' | head -100
Anonymous No.106597149
>>106596920
This is a thread about bash
Anonymous No.106597345
>>106597099
saar, you could do this more efficiently with java or AI.
Anonymous No.106597511 >>106597597
>>106595478
https://github.com/v1cont/yad
some examples here by the creator
https://sanana.kiev.ua/index.php/yad
>Several complex YAD scripts
Anonymous No.106597597
>>106597511
>>Several complex YAD scripts
oh theres more here
https://github.com/v1cont/yad/wiki/

also checkout dmenu, it can be used for quickly inputting something, say input for a script
rofi is a more capable upgrade over dmenu allowing for better algorithm sorting out of the box, displaying images, etc
https://fedops.codeberg.page/dmenurofi-scripts.html