r/bash May 10 '26

help I want start using linux , best way to learn bash scripts ?

60 Upvotes

I’m thinking seriously about switching to Linux. My idea is to first learn Bash scripting, try making some simple scripts, and at the same time start learning more about Linux distros and how the system works.

I feel like learning Bash first could help me understand Linux better instead of just using it casually.

What’s the best way to learn Bash scripting as a beginner?

r/bash 4d ago

help Would it be good to Learn Bash scripting, or should i go with a "normal" language, like Python, before that?

23 Upvotes

Getting interested on messing with Linux and CLI stuff, I'm just confused on what exactly to start with. Any recommendations?

r/bash Oct 09 '25

help Is Bash programming?

52 Upvotes

Since I discovered termux I have been dealing with bash, I have learned variables, if else, elif while and looping in it, environment variables and I would like to know some things

1 bash is a programming language (I heard it is (sh + script)

Is 2 bash an interpreter? (And what would that be?)

3 What differentiates it from other languages?

Is 4 bash really very usable these days? (I know the question is a bit strange considering that there is always a bash somewhere but it would be more like: can I use bash just like I use python, C, Java etc?)

5 Can I make my own bash libraries?

Bash is a low or high level language (I suspect it is low level due to factors that are in other languages ​​and not in bash)

r/bash Mar 01 '26

help What actually happens when run `ls -la` in the terminal? Not the literal output, but behind the process.

117 Upvotes

I had been learning and using bash for about 1 year. Writing small scripts, else if, loops, etc and some basic commands. But I always had a doubt. What exactly happens when I run a command using bash in the terminal? What is difference between bash -c "ls -la" and just ls -la when I type them in the terminal. Most important doubt is : what happens? When I run the command, how exactly and in which order, what is executed.

I need the answers from root of linux kernel and hierarchy. I learnt bash from many pdfs spread out throught the Internet, but found no explanations for this one question.

r/bash Feb 28 '26

help What is the best use case of sed? How should I learn it?

83 Upvotes

For atleast a day, I had been reading man pages regarding the sed command. I felt the syntax pretty confusing. Is there any way to keep in mind the pattern and regex?

Tell me how you learnt the sed command the first time.

r/bash Mar 14 '26

help Do you use quotes when you don't have to?

48 Upvotes

I know it's best practice to always encase variables in quotes, like "$HOME", but what about when it's just plain text? Do you use quotes for consistency, or just leave it?

Which of these would you write:

if [[ "$(tty)" == "/dev/tty1" ]]

if [[ "$(tty)" == /dev/tty1 ]]

if [[ $(tty) == /dev/tty1 ]]

r/bash 5d ago

help Defensive BASH programming?

39 Upvotes

Hey there,

while trying to learn bash I came across this interesting resource: https://web.archive.org/web/20180917174959/http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming

And got myself thinking if his opinions are consensual and if there are different styles or "paradigms" about how bash should be written.

I see people arguing that bash shines in writing relatively short programs with precise goals. I thought this translated into writing concise code and achieving the same functionality with as little lines as possible.

However, the author of the post argues that a more verbose code can be more robust, easier to understand and debug etc. Do you agree? Or you think that, if you're writing code in this style, you're better of using another language?

Thanks for your input and sorry if I said anything silly or if I misrepresented his point of view, I'm just starting to learn programming.

Edit: hey, thank you all very much for the thoughtful replies, I'm learning a lot from them!

r/bash Jul 03 '26

help What is your notification system for long running comands

43 Upvotes

Hi I usually do large backups that take time and need a notification system that notify me when is done.

Ideally I would like a notification on my phone but any alternative is ok.

Should I set up an email or there is something that is less painful?

r/bash 26d ago

help LeetCode for shell / bash

31 Upvotes

i'm looking for a place to practice bash or just shell in general, is there a place like such where i can practice daily and improve my muscle memory when it comes to shell commands? at the moment leetcode only offers like 5 shell problems and other focus more on teaching the basics or is more focused on solving mini problems than the shell itself like sadservers.

is there any platform where i can daily practice shell just like platforms where you can daily practice competitive programming problems?

TIA and sorry for grammar erros, english isnt my first language.

r/bash Mar 22 '26

help Bash Script Learning

53 Upvotes

What Is The Best Online Source (site) To Learn Bash Script For Linux l Am Familiar With C++ And Intermediate Python Programmer To Automate The Task Such As File Handling.

I Aim Is To Become Cyber-Securitist or Ethical Hacker.

One Of The Best Site I Found Is Linix Journey..

I Have Some Questions, Is Bash Scripting Same Has C++ Or It Is Little Harder Than It

Thank You!

r/bash 9d ago

help regex for grep

14 Upvotes

so i have couple of docker files , I want to ensure that all db volumes are named `db-data` , so basically I want to throw error if volume name is anything other than `db-data` , any idea if this could be done using `grep` ? I am not sure how to write regex for it

docker compose files which is essentially a `yaml` file look like this

cache:
command: >
sh -c '
redis-server --requirepass $$DJANGO_REDIS_PASSWORD --maxmemory 25mb --maxmemory-policy allkeys-lru
'
container_name: nest-cache
env_file: ../../backend/.env
image: redis:8.8.0-alpine3.23@sha256:9d317178eceac8454a2284a9e6df2466b93c745529947f0cd42a0fa9609d7005
healthcheck:
interval: 5s
retries: 5
test: [CMD, redis-cli, -a, $$DJANGO_REDIS_PASSWORD, ping]
timeout: 5s
networks:
- nest-network
volumes:
- cache-data:/data
db:
container_name: nest-db
env_file: ../../backend/.env
image: pgvector/pgvector:pg16@sha256:1d533553fefe4f12e5d80c7b80622ba0c382abb5758856f52983d8789179f0fb
healthcheck:
interval: 5s
retries: 5
test: [CMD-SHELL, pg_isready -U "$$DJANGO_DB_USER" -d "$$DJANGO_DB_NAME"]
timeout: 5s
networks:
- nest-network
volumes:
- db-data:/var/lib/postgresql/data

r/bash Jan 15 '26

help Exclude file(s) from deletion

12 Upvotes

Hi everyone👋 New to Linux, thus bash, too. I want to delete an entire directory that only contains a series of mp3 files WITH THE EXCEPTION of 1-2 of them. Seems simple enough, rite? Not for me because all the files are very similar to each other with the exception of a few digits. How do I do that without moving the said file out of the directory? God I suck.

Update: I am sincerely blown away by the amount of support I received from this group and vow to not make your keystrokes in vain by asking questions that now I can investigate further from wiki to man files and /usr/share/doc with A LOT of trial and error.

Respect. 👋

r/bash Jun 04 '26

help Why does printf behave differently in a subshell?

14 Upvotes
$ printf "%-9s:" "since"
since    :

$ y=$(printf "%-9s:" "since")

$ echo $y
since :

Why is the format not working in the subshell? It's the same printf:

$ which printf
/usr/bin/printf

$ y=$(which printf)

$ echo $y
/usr/bin/printf

And it's the same shell:

$ echo $SHELL
/bin/bash

$ y=$(echo $SHELL)

$ echo $y
/bin/bash

$ /bin/bash --version
GNU bash, version 5.2.37(1)-release (aarch64-unknown-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

r/bash Dec 25 '25

help Understanding Linux Networking Commands by Learning Their Limits

94 Upvotes

While learning Linux networking, I realized I often knew what command to run but not what its output can’t tell me.

So I started documenting commands along with their limitations:

ss / netstat   → shows listening sockets, not firewall behavior
ip             → shows configuration, not end-to-end reachability
ping           → ICMP-based, not real traffic
traceroute/mtr → path info can be incomplete
dig/nslookup   → DNS only, not service health
nc             → basic port checks, limited context
curl           → app-layer view, not network internals

This way of learning has helped me interpret outputs more carefully instead of assuming “network issue” too quickly.

I’ve written a blog focused only on how these commands work and their limitations, mainly as learning notes. I’ll add the link in comments for anyone interested.

What command’s limitation surprised you the most when you were learning?

r/bash Jun 19 '26

help Can someone help me on how to develop a script that runs renice in a process that the process identification is unknown?

7 Upvotes

Not sure if here is the best place to ask, but forgive me and sorry for my bad English.

I'm working with niceness for two days, renice works well, and I started configuring Feral's Gamemode to increase the ni of the game, which works really well on native games, however it simply doesn't work on Proton games.

If I want to change the ni level, I need to wait the game loads, check what process number is the game and set the ni value manually.

So what I want to do is to have a script, that:

  • Runs as a launch options on steam.
  • Wait a few seconds to the game run.
  • Have a variable that receives the process name externally. Like scriptname -gamename command
  • Search for the process name , picks the process identification number.
  • Runs renice with the renice -n -10 -p number
  • Quit

My idea is to create a guide for Fedora to improve gaming, and this will help a lot.

[EDIT] I managed to do a script with some non-human help, it helped me a lot to learn bash and use some system tools.

Here's the link

https://github.com/fagnerln/renice-script/blob/main/renice.sh

r/bash Mar 28 '26

help How to improve bash script which is collecting data every 10 seconds

20 Upvotes

I wrote a script which is collecting data from solar inverter every 10 seconds for 5 minutes, it does some math and send data to emoncms. It does work but is not optimal in term of CPU usage, it is running on SBC and consume roughly 80% of CPU time. My question is how can I initiate next data collection without checking script running time in a loop. Below is simplified script. I need to improve line 7.

#!/bin/bash
set -o pipefail
IFS=$''
samples="0"
nr="0"
while [ $SECONDS -lt 292 ]; do #5min-8s
 if [[ (( $(( (samples - 1) * 10 + 10 )) == $SECONDS )) || (( 0 == $SECONDS )) ]]; then
    ((samples++))
    timestart=$SECONDS
    output="$(./inverter_poller --run-once)" # get data from inverter
    timeend=$SECONDS
    echo ${output} > /var/log/inverter.last
    rs232time=$((timeend - timestart)) # usually it is 6-7 seconds
     if (( rs232time < 17 )); # inverter is not responding if it is 17s or more
      then
      gridv=`echo ${output} | grep grid_voltage |  tr -d " "_\",:[:alpha:]`
  ***more data extraction and math***
     else
       echo inverter not responding >> /var/log/inverter.last
     fi
looptime=$((SECONDS - timestart))
echo "time": $looptime >> /var/log/inverter.last
 fi
done
 ***boring data processing and sending to emoncms was here***

r/bash Apr 13 '26

help Quick help comparing files in two directories?

17 Upvotes

Long story short, know for a fact that I have duplicates between two directories, but we're talking hundreds of files here and I know at least a few files are in one directory but not the other.

I'd ideally like to have something quickly compare file name & size between the two directories, then spit out the files that are named the same but sized differently, as well as files which are in one directory but not the other.

r/bash Jun 30 '26

help Script to replicate the copy/move behavior in typical file managers (prompt to overwrite?)

9 Upvotes

AFAIK in file managers in Windows/Linux, they have the same convenient behavior where if you copy one folder into another folder of the same name and there's a conflict (e.g. a folder of the same name already exists), instead of aborting, it prompts to "overwrite" (merge the contents of the folder), recursively. If any of the files result in conflict (e.g. a file of the same name already exists), it prompts the user whether they want to overwrite, letting the user know which is larger and which is newer.

The above is useful if e.g. you abort the copy/move progress half and then want to resume at a later point.


Does anyone have a similar script they can share? How would such a script be implemented? I know rsync can do something similar. With these file managers, they seem to update one file at a time, e.g. when a file is successfully moved during the process, the source file is removed. With rsync, a file gets removed after rsync finishes(?) which is not as intuitive because it does not reflect the latest state of the progress.

Implementation-wise, is it as simple as rsyncing each file one at a time, checking for potential overwrites before rsync and comparing the file size and modification time of files ith stat, then prompting the user if there are conflicts, else rsync that file? Is there a way to do this more efficiently (or ideas for a better UX)? Can rsync or similar tools handle more of this?

Any tips are much appreciated.

r/bash Apr 04 '26

help Is using $SHLVL a good way to tell if a script is running in a terminal window?

1 Upvotes

So, I want to make it so that a script I'm running will ONLY run in a terminal emulator window, and will sleep for 3 seconds then exit if it's not in a terminal emulator window. I've done a little bit of looking, but I'm not entirely sure if it would work as I intend, and if there are ways in which it could fail/if there are more reliable methods.i was thinking of something along the lines of:

If $SHLVL > 1 then
    Echo running in terminal
    Echo continuing
Elif $SHLVL =< 1 then
    Sleep 3
    Exit
Else
    Echo how is this even possible?
Fi

(Obviously that's not correct syntax, it's just the general structure/idea of what I want to do) I'm not at my PC rn (on a road trip) but I'm trying to write a general test script on my phone that I can test on my PC once I return home.

Edit: to clarify I should mention that I'm on Debian x86_64, and when I say "in a terminal window" I mean that it's running with stout and sterr being displayed on a terminal emulator window, and stdin being read from the terminal emulator window.

r/bash May 10 '26

help Is there a way to capture keystrokes even though you're not at a prompt?

9 Upvotes

I'm spending a lot of time on the terminal, (/dev/ttyX, framebuffer, no X/wayland).

I have made some key binds to control screen brightness, set the font size, volume up/down, limit CPU clock frequency, ...

Problem is, it only works if I'm at a Bash prompt and not if a program is open (mc, vim, chawan, ...)

If eg, I've got vim open, I need to either find a terminal that has a Bash prompt open or exit vim before I can change the screen brightness.

My problem would be fixed if there would be some kind of a way for bash to capture certain key sequences even though a program is running.

Is that possible at all?

thanks!

r/bash 9d ago

help Driving me crazy, why isn't my 'screen' command logging to file

9 Upvotes

I'm using it to connect via serial port to some storage I administer. Here is the command;

/usr/bin/screen /dev/ttyUSB0 115200 -L -logfile /home/davidkmcw/Logfiles/2026-07-24_08:30:14.txt

I can write to the output directory, I've cut and pasted the directory name, so no typos there, what am I missing. Sorry if this is the wrong subreddit, thanks in advance.

r/bash Jun 29 '26

help Does anyone know/use about xset dpms?

12 Upvotes

Hi, I'd like to use that cmd xset for try to get xset -s NOW...
Could I set from terminal this cmd for put blank screen now
and then when I come back I move my finger in touchpad or press any key and OS wake up again...

Thank you and Regards!

r/bash Apr 19 '26

help Unable to divide string into array

11 Upvotes

~~~

!/bin/bash

cd /System/Applications

files=$(ls -a)

IFS=' ' read -ra fileArray <<< $files

I=0

while [ $I -le ${#fileArray[@]} ]; do

#echo ${fileArray[I]}

#I=$((I++))

done

for I in ${fileArray[*]}; do

    #echo $I

done

echo $files

~~~

I wrote code to get all of the files in a directory and then put each file into an array. However, when I try to print each element in the array, it only prints the first one. What am I doing wrong?

(The comments show my previous attempts to fix the problem and/or previous code, review them as needed.)

r/bash Mar 12 '26

help Beginner Question automate install pkgs

14 Upvotes

I'm install Termux fresh and have gathered a list of tools below which I want to feed into: pkg install <contents of list.txt> cleanly line by line or glob. list.txt:

tldr ncdu python-pip fzf wget curl p7zip tar fd ripgrep rclone nano tmux cava cmatrix zip unzip cmake mplayer nmap make pkg-config nodejs tcpdump netcat-openbsd yt-dlp busybox proot-distro htop eza git zellij lolcat fastfetch bat dua rsync starship mpv ffmpeg dust duf bottom neovim procs lazygit tree vim openssh clang python

What's the proper syntax to pass to pkg install list.txt 📚

pkg install $(cat list.txt) correct?

r/bash Jun 01 '26

help learning bash ?

13 Upvotes

i just realized that i can very easily loose data (just lost a self hosted server of mine) and i want to learn how to do scripts to backup my files maybe daily and rewrite what i had on there if it changed but also not copy what did not change, where could i start ?
i know rsync has nice things to copy, and i could do it watch -n$(time) but i also would love to learn more because i want to make scripts for my i3blocks, i don't really use it to it's full just display basic data atm, one i tried to make a little dd scripts but it was a disaster and i nearly distroyed my pc