[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: bash.jpg (123 KB, 2000x1200)
123 KB
123 KB JPG
>actually learning bash
yay or nay chat?
>>
>>106594442
yay is to download from the aur on arch
nyay is to download from the aur on nyarch
>>
>>106594442
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.
>>
>>106594492
I use python if I want to make some scripts from time to time. but bash seems more based
>>
Definitely yes if you use a linux distro. It comes in very handy. For any complex scripting, I combine bash and python.
>>
Why not? Gluing programs together is quite useful.
>>
>>106594442
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
>>
>>106594442
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
>>
>>106594526
Bash kinda feels like Python's retarded little brother (the syntax feels pretty clunky) but it's better at some things
>>
>>106594526
There is quite literally nothing based about bash.
>>
>>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
>>
>>106594442
>>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)
>>
>>106594442
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
>>
>>106594442
you will probably spend more time learning the gnu core tools than the syntax of bash because those are what you actually use.
>>
>google bash cheat sheet
congrats you can now write scripts
>>
>>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
>>
>>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
>>
>>106594442
I just use fish
>>
>>106595303
What makes fish better?
>>
>>106594442
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.
>>
>>106595325
It's more friendly, easier to read, etc. But you can have more than one shell for different tasks, same for programming languages.
>>
>>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.
>>
>>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.
>>
>>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
>>
File: 1747608190734897.png (41 KB, 341x510)
41 KB
41 KB PNG
>>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.
>>
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
>>
>>106595333
In my case I rarely use awk/sed/cut at all, bash does almost everything
>>
>>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
>>
>>106594442
>bash
you should make POSIX compliant scripts instead
>>
>>106595566
>September 20, 2011 — Carlos Fenollosa
in 10 more years its going to be
>1500 LOC
>>
>>106594442
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
>>
>>106595632
Also SGR sequences to give it all a pop-a-color
https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
>>
>>106595566
uh why's he using a billion echos when one heredoc will do?
>>
>>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
>>
>>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.
>>
>>106595566
https://github.com/angristan/openvpn-install/blob/master/openvpn-install.sh
>1383 LOC
>>
>>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/
>>
>>106594442
Dont expect bash to be like other languages but its rly fun if u actually learn it.
>>
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
>>
File: shell-startup-actual.png (242 KB, 1558x955)
242 KB
242 KB PNG
>>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
>>
>>106594442
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.
>>
>>106595768
>https://github.com/dylanaraps/pure-bash-bible
downgrade that to
https://github.com/dylanaraps/pure-sh-bible
>>
Posix shell and guile scheme is all you need.
>>
>>106595803
>but for anything longer that you intend to actually maintain it becomes a pain.
you're weak
>>
>>106594442
of course you should learn bash. learn the tools of your trade.
>>
>>106594442
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
>>
>>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
>>
>>106595183
cis
>>106595241
>>106595250
trans

need I say more?
>>
>>106596185
>>106596205
good morning sar
>>
>>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
>>
>>106596229
explain how shredding laurie makes me trans
>>
>>106596229
>using the source program is trans
damnit its so over bash bros
>>
>>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.
>>
>>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
>>
>>106596236
Indians write Python CLIs that have miserable UX.
>>
diff <(ls dir1) <(ls dir2)

Learn process substitution, it will change your life
>>
>>106596889
Feeding "ls" into anything is cringe and nigger tier.
>>
>>106596889
SC3001: In POSIX sh, process substitution is undefined.
SC3001: In POSIX sh, process substitution is undefined.
>>
>>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
>>
>>106596920
This is a thread about bash
>>
>>106597099
saar, you could do this more efficiently with java or AI.
>>
>>106595478
https://github.com/v1cont/yad
some examples here by the creator
https://sanana.kiev.ua/index.php/yad
>Several complex YAD scripts
>>
>>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
>>
>>106595732
>script that creates scripts based on choices
yikes, that's one hella ugly code. are all complex bash scripts like this?
>>
>>106594442
Echo poop sandwich
That’s all the bash I know lmao
>>
Handy if you use linux. Relatively easy to learn
>>
>>106596246
You're trans.
>>
If you use FFMpeg on Linux, defintely a must.
>>
>>106594442
Yay if you want to automate shit like backups or system monitoring or just like to play around and get to know the system.
Yay anyway.
>>
>>106594442
For a Linux system knowing Bash is invaluable, you're gonna unlock so many options for yourself.
>>
Bash scripts are best left to a level of complexity that ai works well at, while having an arcane syntax, so it’s not as worth learning as it used to be.

Yet it’s like networking, pretty much unavoidable. Unlike networking bash is not that complicated you can legit learn the entire thing if you’re so inclined. It’s also not going anywhere although you’ll probably see more zsh. I don’t think writing sh is worth it.

It is also surprisingly easy to pwn yourself with bash so learning some application security can be prudent. AI will tell you to do stupid things.
>>
>>106594442
assuming you mean "yea or nay", yea
>>
File: soybash.png (288 KB, 1090x981)
288 KB
288 KB PNG
>>106594442
BASH is not a real programming language, but most people are not ready to have this discussion.
>>
yay.

Copy and paste this into your ~/.inputrc file to configure GNU readline (used by bash)

I included comments to explain what they do.

# ~/.inputrc
# See 'man bash' - 'Readline Variables' for more details on each variable
# sources for minimum versions:
# - <https://tiswww.case.edu/php/chet/readline/CHANGES>
# - <https://cgit.git.savannah.gnu.org/cgit/readline.git/tree/CHANGES>
# - <https://cgit.git.savannah.gnu.org/cgit/bash.git/tree/CHANGES>
# sources for release dates:
# - <https://ftp.gnu.org/gnu/readline>


# use distinct colors for each file type in list of completion options
# - requires v6.3 (Feb 2014)
set colored-stats on

# case insensitive
# - requires v2.2 (Apr 1998)
set completion-ignore-case on

# max number of completion items shown automatically before prompting user y/n
# - requires v2.0 (Aug 1994)
set completion-query-items 200

# treat hyphen (-) and underscore (_) as equivalent
# - requires v6.2 (Feb 2011)
set completion-map-case on

# first show the common prefix between ambiguous options before cycling
# - requires v6.2 (Feb 2011)
set menu-complete-display-prefix on

# if completion is ambiguous, immediately show the list of options
# - requires v4.0 (Feb 1999)
set show-all-if-ambiguous on
# skip over already matching text when TAB-completing in the middle of a word

# e.g. TAB after 'e' in 'Makefile' gives 'Makefile' not 'Makefilefile'
# - requires v6.1 (Jan 2010)
set skip-completed-text on

# colorize the matching prefix in list of completion options
# - requires v7.0 (Sep 2016) (Ubuntu 18, RHEL 8)
set colored-completion-prefix on

# (ctrl+space): show all possible tab completions
# - requires v2.1 (Jun 1997)
"\000": possible-completions

# (TAB): change to menu based cyclical completion
# - requires v2.2 (Apr 1998)
"\t": menu-complete

# (Shift+TAB): cycle backward to the last option
# - requires v6.1 (Jan 2010)
"\e[Z": menu-complete-backward

# disable the annoying bell
# - requires v2.0 (Aug 1994)
set bell-style none
>>
>>106595150
>bash + curl + REST API + jq + sed, awk, grep
im not a programmer or anything but learning this stack has made me a god at work and they dont even know I use this shit. its bushleague tier employment but I make ok money for what it is. and AI is pretty good at helping with this stuff too so... its easy if you can break it down into bits
>>
>>106600276
Protip: you can set your Readline options in .bashrc with "bind".
For example
>set colored-stats on
>"\000": possible-completions
becomes
>bind "set colored-stats on"
>bind '"\000":possible-completions'
in .bashrc

I find it easier to manage as it's one less config file to worry about.
>>
>>106594442
>>106595141
many programs just have a CLI instead of developing and maintaining a framework for a programming language like Python

even in the cases where they do have a Python library, the CLI is often more complete

bash lets you combine multiple commands in a programmatic way. you should have already learned it. if you don't know bash, learning containerization will be much more difficult.
>>
You don't learn bash nigga
You install shellcheck and its corresponding plugin in your IDE, and you write scripts that do very targeted things. If you need more complex stuff you use a proper programming language.
That's it.

souce: 16 years "of bash" fixing the shit you fuckers made
>>
>>106600511
>shellcheck
opinion discarded
>>
You only need the bible.
>>
>>106600449
Yes I'm aware of that but then they are only set for bash.

Setting them in ~/.inputrc works for every program that uses GNU readline.
>>
>>106596567
like perl
>>
>>106594526
bash is unique in that it's only really good at taping together already existing programs so you can quickly do what you want
if you're trying to invent a program with specific functionality or one that needs traditional programming logic, don't use bash
>>
>>106596237
>eshell in emacs
Eshell scripts are based. You can run them headless in batch mode running in a remote emacs server. Magic.
>>
unless you are a sysadmin nay. just learn posix or plan9 rc if you are neeting
>>
>>106594442
Essential if you use any *NIX system
Anybody who disagrees is probably an Applefaggot
>>
>>106601994
>Essential if you use any *NIX system
yes, posix
>>
>>106602001
>posix sh
is a subset of bash
they differ rarely
>>
>>106602015
>rarely
entirely different math notation
>>
>>106602024
such as?
>>
>>106594442
YAY
>>106594450
>nyarch
I thought you're trolling
>>
>>106599507
I will never be a real woman.



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.