/r/bash

Photograph via snooOG

Wake me up when September ends.

A subreddit dedicated to Bash scripting. Now complete with a Discord Server.

Rules

  1. Content must be Bash related. This rule is interpreted generously; general shell scripting content is mostly accepted. However, the post should not be specific to another shell.

  2. No reposts. This is meant with regards to content, not just “the same link was submitted earlier” – it’s okay to resubmit an old link in some new context (e. g. because you’d like to discuss another part of it, or because something has changed since the last time it was submitted, or because the link was updated since then). Links from the sidebar count as having been submitted already, so posting them without new context is also considered a repost.

Flair

You can choose one of these four flairs for your post:

  • Help – You seek help, or want to ask a question. “help” posts are usually self posts, though you may also submit a link to a thread in a different subreddit (e. g. /r/commandline).
  • Solved – The submission used to be flaired as “help”, but your problem has been solved, or your question has been answered.
  • Submission – General submission of any kind (link or text post).
  • Critique – You are submitting a creation of your own (usually a Bash script) and actively seek feedback on it and how to improve it.

If you don’t flair your post, the moderators will set the most appropriate flair.

Related subreddits

Guides

Other resources

  • Explainshell - Explain complex shell operations.

  • ShellCheck – Automatically detects problems with shell scripts.

  • BashFAQ – Answers most of your questions.

  • BashPitfalls – Lists the common pitfalls beginners fall into, and how to avoid them.

  • (Archived) The Bash-Hackers Wiki – Extensive resource.

  • #bash – IRC channel on Libera. The main contributors of the BashGuide, BashFAQ, BashPitfalls and ShellCheck hang around there.

/r/bash

67,689 Subscribers

8

Article about Bash Strict Mode

I write an article about Bash Strict Mode.

I would love to hear your feedback:

https://github.com/guettli/bash-strict-mode

9 Comments
2024/11/08
22:04 UTC

1

Filter video files by codec?

How to list and sort/move video files by codec?

Ideally just move all have/x265 files into a subfolder kinda thing.

I've just grabbed mediainfo and am prodding at but don't really know what I'm doing....I suspect with a little bash and some knowledge it should make this simple.

2 Comments
2024/11/08
21:21 UTC

16

What terminal app do you use on your Linux distro

Hello everyone,

I am a Mac user, and there is a long time I don't use a Linux distribution. My terminal app is iTerm2. What is the best replacement for iTerm on Linux distro, such as, Ubuntu/Fedora/Alpine ?

My requirements are 256xterm colors and font change ability.
Thanks.

51 Comments
2024/11/08
16:27 UTC

3

Looking for some guidance on how to tackle this problem with bash. Had a few ideas that I think can work but would like more experienced opinions.

Hello! For context to the title, I'm trying to make a bash script(s) that use the ncat util from nmap to capture data being streamed by a phone server, that part I've been able to accomplish no problem. I'm running into an issue when trying to take that data capture and output it to unique daily files, and having those files have only the output for that specific day.

The issues i've ran into are:

  • if I move the file where ncat is being redirected to, it keeps appending to that same file even after renaming
  • if I try to have the script running ncat change the file name to make new output file every day, no dice.
  • using a cron job worked to an extent, in that scenario the ncat command was still putting everything in 1 big file and that was what was copied daily.

How would you approach something like this??

I've changed my scripts around a lot and the concepts are pretty simple so I haven't pasted my code here in the post, but if you'd like to see some of it by all means just ask :)

One thought I had was to make the script running ncat in a way where it can be called by a cron job, the only issue I can see there is making sure the ncat process stops after the day's over.

6 Comments
2024/11/08
16:04 UTC

3

When a process is killed because it exhausted free memory, I'd prefer bash says "Killed: out of memory" instead of just "Killed"

I see in siglist.c the internationalized string:

sys_siglist[SIGKILL] = _("Killed");

But I'm wondering if we can use anything that the kernel does around https://github.com/torvalds/linux/blob/master/mm/oom_kill.c#L947 to tell the user that the reason was low memory?

10 Comments
2024/11/08
10:20 UTC

0

ImageMagick6: ¿how change save 75 compr.(default) to 95 compr.?

Hi, this ask is about ImageMagic 6: Do you know how I change the compression for save by default is 75 and I'd like to set compression 95 (so change 75 for 95 by default).

Thank you and Regards!

4 Comments
2024/11/08
00:25 UTC

6

Learning more practical automation

Can anyone point me to where I can learn more real world scripting. More so applying updates to things or monitoring system health, so far all of the “courses” don’t really help more than understanding simple concepts.

27 Comments
2024/11/07
21:19 UTC

7

Simple bash script help

Looking to create a very simple script to start a few services at once just for ease. My issue is it only wants to run one or the other. I'm assuming because they're both trying to run in the same shell? Right now I just have

cd ~/path/to/file &
./run.sh &
sudo npm run dev

As it sits, it just starts up the npm server. If I delete that line, it runs the initial bash script fine. How do I make it run the first script, then open a new shell and start the npm server?

14 Comments
2024/11/06
17:19 UTC

1

Bash grep

I've tried numerous rules, but I can't seem to figure out the correct pattern.

I have numerous words in a file

abcd:1234:ef35
f24a:5fa3
ab12:34fa:cd42

I'm trying to write a grep that if a word contains more than 1 colon, the entire word is returned.

The issue is that the strings may be in a list, or they may be side-by-side, only separated by a space

abcd:1234:ef35 f24a:5fa3 ab12:34fa:cd42

I can't seem to pull the whole word, it always returns a single character.

20 Comments
2024/11/06
00:32 UTC

1

Who else has something like this in their .rc?

15 Comments
2024/11/05
16:19 UTC

1

Pass delimited string variable-array directly into for loop?

I successfully followed instructions at this StackOverflow post to convert a string variable, var="a,b,c" to a 3 element array ignoring the commas:
arrIN=(${IN//,/ })

for i in "${arrIN[@]}"; do 
    echo "$i"; 
done

I would like to place command right after i in:
Neither of the following worked:

for i in "${(${IN//,/ })[@]}"; do 
    echo "$i"; 
done
Error: bash: ${(${IN//,/ })[@]}: bad substitution

Same error when I removed the the parentheses, ( ).

3 Comments
2024/11/01
16:43 UTC

1

Temporarily change terminal [16] color palette in a script?

What's the specific term to call/describe the 16 colors that's always being used by the terminal? (neofetch colored squares, etc.)

And is there a way to dynamically change them through a script?

Searching for solutions, not sure if the command I need is tput or dircolors or something else.

Why do I want to do this? One utility I'm using will only use the set of 16 colors used by the terminal. I'm looking for a workaround so that I can force it to use colors I specify (from the 256 color set) without changing the defaults of my terminal.

3 Comments
2024/11/01
12:14 UTC

1

Trying to understand why my search returns no results

Hi all,

Let me preface this by saying this is day one of my scripting journey. I'll also add that I am using ChatGPT to try to cheat the hell out of it!

With that said, here is my problem. I am going step by step through the process of converting a PDF to text, parsing the text for info and then saving that info into a csv file.

I am on OSX so I started by using Shortcuts to "Get text from PDF" which i initially outputted to a text file; it worked fine. I then added a script (generated by ChatGPT) to search the clipboard (I changed the Shortcuts output to the clipboard) for the line "Grand Total" and output the line below (which had the amount) to a csv file. However the script can't find the line "Grand Total". Ive tried this initially with Applescript and now with a shell script, neither work.

Here is the code I'm using:

#!/bin/bash

echo "pbpaste version: $(pbpaste)"

# Get the clipboard contents
input_text=$(pbpaste)

# Convert the clipboard text into an array of lines
mapfile -t lines <<< "$input_text"

# Initialize variables
grand_total_found=false
grand_total_value=""

# Loop through each line
for ((i = 0; i < ${#lines[@]}; i++)); do
    # Normalize the line by trimming spaces and converting to lowercase
    current_line=$(echo "${lines[i]}" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr '[:upper:]' '[:lower:]')

    # Check if line contains "grand total" (case-insensitive)
    if [[ "$current_line" == *"grand total"* ]]; then
        # Get the next line for the total amount and trim whitespace
        grand_total_value=$(echo "${lines[i+1]}" | sed 's/^[ \t]*//;s/[ \t]*$//')
        grand_total_found=true
        break
    fi
done

# Check if "Grand Total" was found
if [ "$grand_total_found" = true ]; then
    # Prompt for output file location
    echo "Enter the path to save the CSV file (e.g., /path/to/output.csv):"
    read -r output_file

    # Write "Grand Total" and value to the CSV file
    echo "Grand Total,$grand_total_value" > "$output_file"
    echo "Grand Total saved to CSV successfully at $output_file"
else
    echo "No 'Grand Total' found in the clipboard text."
fi

And here is the output from that currrently:

pbpaste version: Remittance Statement

1.00

To:

REDACTED

Date: 31/10/2024

Ref: TR16246

(Property) REDACTED

Date Main Tenant Description VAT

Charge

(inc VAT)

Payment

(inc VAT)

29/11/2022 Not Applicable 392208 7,850.29

0.00

7,850.29

0.00

Grand Total

7,850.29

REDACTED

E-mail: REDACTED

VAT No: REDACTED

Page 1 of 2

Income and Expenditure

Type VAT

Charges

(inc VAT)

c000- Contractor Charge 7,850.29

Payments

(inc VAT)

Totals

7,850.29

Total Remitted 7,850.29

REDACTED

Page 2 of 2

No 'Grand Total' found in the clipboard text.

I added the echo just to review the text it was taking from the clipboard was correct.

Any help at this basic stage much appreciated as this is going to get more complicated (I'll eventually need to output multiple lines). Also, what are the best places to look for documentation onn this sort of stuff?

Thanks all.

0 Comments
2024/10/31
15:44 UTC

0

Help (Newbie)

if i gonna learning bash scripting, where to start and how?. i know understand bash scripting, but can'not make it myself

13 Comments
2024/10/31
13:08 UTC

6

File names with spaces as arguments

I want to merge a bunch of PDF s. The file names have spaces : a 1.pdf, b 2.pdf, a 3.pdf. And they're a lot of them.

I tried this script:

merge $@

And called it with merge.sh *.pdf

The script got each separated character as an argument : a 1.pdf b 2.pdf a 3.pdf.

I there a way to feed these file names without having to enclose each in quotes?

16 Comments
2024/10/30
23:21 UTC

1

M3U file list

I know I can create a file list with ls -1 > filename.txt, but I don't know how to prepend the directory path. I'm trying to create an m3u file list I can transfer to Musicolet on my phone. Can someone point me in the right direction?

2 Comments
2024/10/30
11:22 UTC

8

cat match string / move to end of file

i've been over a few different websites reading up on this, but I feel like I'm missing something stupid.

I have a file, which contains a mix of ipv4 and ipv6 addresses. I'd like to use sed to match all ipv6 addresses in the file, cut them from their current position, and move them to the end of the file.

I've tried a few ways to do this, including using cat to read in the file, then using sed to do the action. It seems to be finding the right lines, but I read online that /d should be delete, and I'm trying to just get that to work before I even try to append to the end of the file.

cat iplist.txt | sed -n "/::/d"

I haven't even figured out the part of appending to the end of the file yet, I just wanted to get it to delete the right lines, and then add it back

cat iplist.txt | sed -n "/::/d" >> iplist.txt
15 Comments
2024/10/30
06:50 UTC

1

Issues when customizing LS_COLORS

Hello everyone,

I recently parametered my .bashrc file to customize my ls command colors. But some file types appear in two different colors, when I only put one in my .bashrc. Example with my .md files, which are supposed to be light blue but also appear hot pink :

https://preview.redd.it/71vj5kls6qxd1.png?width=569&format=png&auto=webp&s=c288ee9581663d3bd247184eebbc9a8410f3e667

Here are my parameters in my .bashrc :
LS_COLORS="di=1;38;5;218:*.sh=1;38;5;213:*.tar=1;38;5;205:*.zip=1;38;5;205:*.gz=1;38;5;205:*.bz2=1;38;5;205:ln=1;38;5;218:*.docx=1;38;5;174:*.doc=1;38;5;174:*.pdf=1;38;5;174:*.jpg=1;38;5;174:*.png=1;38;5;174:*.jpeg=1;38;5;174:ex=1;38;5;198:*.md=1;38;5;153"

I did not modify anything else in any other file. Is there anything I'm missing? How can I make my files the right color?

0 Comments
2024/10/29
16:58 UTC

1

Color in prompt

I recently moved from powershell to bash and installed starship. My question is how can I make prompt to be not just white text:

https://preview.redd.it/h30nwaem7oxd1.png?width=918&format=png&auto=webp&s=83fe0e408da06de31a3e5d2f1fc6616f81ef45ce

but instead change color like in powershell?

https://preview.redd.it/01x6w2eq7oxd1.png?width=438&format=png&auto=webp&s=057aa79163fab217974bcda86b9af21360ba78b5

0 Comments
2024/10/29
10:16 UTC

9

sensors_t: a simple bash function for monitoring the temperature of various system components with sensors

LINK TO THE CODE ON GITHUB

sensors_t is a fairly short and simple bash function that makes it easy to monitor temperatures for your CPU and other various system components using sensors (from the lm_sensors package).


###FEATURES

sensors_t is not drastically different than a simple infinite loop that repeatedly calls sensors; sleep 1, but sensors_t does a few extra things:

  1. sensors_t "cleans up" the output from sensors a bit, distilling it down to the sensor group name and the actual sensor outputs that report a temperature or a fan/pump RPM speed.
  2. for each temperature reported, sensors_t keeps track of the maximum temperature seen since it started running, and adds this info to the end of the line in the displayed sensors output.
  3. sensors_t attempts to identify which temperatures are from the CPU (package or individual coreS), and adds a line showing the single hottest temperature from the CPU.^(1)
  4. if you have a nvidia GPU and have nvidia-smi available, sensors_t will ue it to get the GPU temp and adds a line displaying it.^(2)

NOTE: the only systems I have available to test sensors_t use older (pre-p/e-core) intel CPU's and nvidia GPU's.

^(1)This (identifying which sensors are from the CPU) assumes that [only] these lines all begin with either "Core" or "Package". This assumption may not be true for all CPU's, meaning the "hottest core temp" line may not work on some CPU's. If it doesnt work and you leave your CPU name and the output from calling sensors I'll try to add in support for that CPU.

^(2)If someone with an AMD or intel GPU can provide a 1-liner to get the GPU temp, i'll try to incorporate it and add in support for non-nvidia GPU's too.


###USAGE

Usage is very simple: source the sensors_t.bash script, then run

sensors_t [N] [CHIP(S)]

N is an optional input to change the waiting period between updates (default is 1 second). If provided it must be the 1st argument.

CHIP(S) are optional inputs to limit which sensor chips have their data displayed (default is to omit this and display all sensors temp data). To see possible values for CHIP(S), first run sensors_t without this parameter.

# example invocations
sensors_t                            # 1 second updates, all sensors
sensors_t 5                          # 5 second updates, all sensors
sensors_t coretemp-isa-0000          # 1 second updates, only CPU temp sensors

###EXAMPLE OUTPUT PAGE

___________________________________________
___________________________________________

Monitor has been running for:  173 seconds
-------------------------------------------

----------------
coretemp-isa-0000
----------------
Package id 0:  +46.0°C  ( MAX = +98.0°C )
Core 0:        +46.0°C  ( MAX = +81.0°C )
Core 1:        +46.0°C  ( MAX = +88.0°C )
Core 2:        +48.0°C  ( MAX = +87.0°C )
Core 3:        +45.0°C  ( MAX = +98.0°C )
Core 4:        +43.0°C  ( MAX = +91.0°C )
Core 5:        +45.0°C  ( MAX = +99.0°C )
Core 6:        +45.0°C  ( MAX = +82.0°C )
Core 8:        +44.0°C  ( MAX = +84.0°C )
Core 9:        +43.0°C  ( MAX = +90.0°C )
Core 10:       +43.0°C  ( MAX = +93.0°C )
Core 11:       +44.0°C  ( MAX = +80.0°C )
Core 12:       +43.0°C  ( MAX = +93.0°C )
Core 13:       +46.0°C  ( MAX = +79.0°C )
Core 14:       +44.0°C  ( MAX = +81.0°C )

----------------
kraken2-hid-3-1
----------------
Fan:            0 RPM
Pump:        2826 RPM
Coolant:      +45.1°C  ( MAX = +45.4°C )

----------------
nvme-pci-0c00
----------------
Composite:    +42.9°C  ( MAX = +46.9°C )

----------------
enp10s0-pci-0a00
----------------
MAC Temperature:  +53.9°C  ( MAX = +59.3°C )

----------------
nvme-pci-b300
----------------
Composite:    +40.9°C  ( MAX = +42.9°C )
Sensor 1:     +40.9°C  ( MAX = +42.9°C )
Sensor 2:     +42.9°C  ( MAX = +48.9°C )

----------------
nvme-pci-0200
----------------
Composite:    +37.9°C  ( MAX = +39.9°C )

----------------
Additional Temps
----------------
CPU HOT TEMP: +48.0°C  ( CPU HOT MAX = +99.0°C )
GPU TEMP:     +36.0°C  ( GPU MAX = 39.0°C )

----------------
----------------    

I hope some of you find this useful. Feel free to leave comments / questions / suggestions / bug reports.

2 Comments
2024/10/27
15:19 UTC

0

What is it called when you ad an interface tu your terminal?

I apologize if this isn't the right sub but I do plan on using bash to do this. So I can use it across platforms. I'm trying to figure out what it's called, as I don't think shell is the proper term. And visor seems unrelated, Basically something with buttons for functions that sticks around at the top of terminals active area, active just meaning the space you can change the color of and nowhere outside it. ?

Thing is I don't want any input or output going underneath the buttons, which I want to use ANSI for. To me I would just called it an interface but that's way too vague, and it would be way too little to call a shell.

Like it would look similar to a HUD placed on you terminal, with active areas you could click with HID, any idea what this is called?

11 Comments
2024/10/27
11:18 UTC

0

Would you consider these silly aliases?

alias vi="test -f ./.vim/viminfo.vim && VIMINFO=./.vim/viminfo.vim || VIMINFO=~/.viminfo; vim -i \$VIMINFO"

alias make='vim Makefile && make'

The first one is so that I don't have my registers for prose-writing available whenever I'm doing Python stuff, and vice versa.

The second one is basically akin to git commit.

5 Comments
2024/10/27
01:21 UTC

3

bash: java: command not found

My Linux distro is Debian 12.7.0, 64bit, English.

I modified the guide titled How to install Java JDK 21 or OpenJDK 21 on Debian 12 so that I could "install"/use the latest production-ready release of OpenJDK 23.0.1 (FYI Debian's official repos contain OpenJDK 17 which is outdated for my use.)

I clicked the link https://download.java.net/java/GA/jdk23.0.1/c28985cbf10d4e648e4004050f8781aa/11/GPL/openjdk-23.0.1_linux-x64_bin.tar.gz to download the software to my computer.

Next I extracted the zipped file using the below command:

tar xvf openjdk-23.0.1_linux-x64_bin.tar.gz

A new directory was created on my device. It is called jdk-23.0.1

I copied said directory to /usr/local

sudo cp -r jdk-23.0.1 /usr/local

I created a new source script to set the Java environment by issuing the following command:

su -i
tee -a /etc/profile.d/jdk23.0.1.sh<<EOF
> export JAVA_HOME=/usr/local/jdk-23.0.1
> export PATH=$PATH:$JAVA_HOME/bin
> EOF

After having done the above, I opened jdk23.0.1.sh using FeatherPad and the contents showed the following:

export JAVA_HOME=/usr/local/jdk-23.0.1
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin

Based on the guide, I typed the following command:

source /etc/profile.d/jdk23.0.1.sh

To check the OpenJDK version on my computer, I typed:

java --version

An error message appeared:

bash: java: command not found

Could someone show me what I did wrong please? Thanks.

11 Comments
2024/10/26
12:13 UTC

Back To Top