/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

70,309 Subscribers

4

Is there a way to get History without <enter>?

Hi, I'd like to get a past command of history for example !1900 but without enter, so I can rewrite that command for this instance and then manually I will do then <enter> for this new changed command?

Regards!

8 Comments
2025/02/04
00:54 UTC

3

nesting command substitutions

My goal is to use dmenu to browse a small set of applications. A list of such applications is in ~/prj/dmenus/favorites/a.txt. If I invoke $(cat ~/prj/dmenus/favorites/a.txt | dmenu)

I get just what I'm after. If I invoke

$(cat ~/prj/dmenus/favorites/a.txt | dmenu -fn 'Droid Sans Mono-18')

I get a output that is nicer to read. Next step, I would like to put the formatting options in a file. I can access that file and read it into a variable by another command substitution.

Example:x=$(<~/.config/dmenu/layout.txt); echo $x yields -fn 'Droid Sans Mono-18'

That is as far as I get. Can't seem to execute in the out command substitution.

$(cat ~/prj/dmenus/favorites/a.txt | dmenu $x)

usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]

[-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]

Not what I want Similarly, if I use

$(cat ~/prj/dmenus/favorites/a.txt | dmenu $(<~/.config/dmenu/layout.txt))

usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]

[-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]

Same failure. I bet the solution is really simple, and will enlighten me immensely.

I am using ubuntu 24.04 with fluxbox.

Thanks

Ti

11 Comments
2025/02/03
23:59 UTC

12

can you explain what this does?

echo '[q]sa[ln0=aln256%Pln256/snlbx]sb5567320342535949633984860024054390510049758475925810612727383477870370412074937779308150930912981042snlbxq'|dc

(It is in a single line)

17 Comments
2025/02/03
05:29 UTC

1

How would you efficiently process every line in a file? while read is 70x slower than Python

I have written a lot of shell scripts over the years and in most cases for parsing and analyzing text I just pipe things around to grep, sed, cut, tr, awk and friends. The processing speeds are really fast in those cases.

I ended up writing a pretty substantial shell script and now after seeding its data source with around 1,000 items I'm noticing things are slow enough that I'm thinking about rewriting it in Python but I figured I'd post this to see if anyone has any ideas on how to improve it. Using Bash 4+ features is fine.

I've isolated the slowness down to Bash looping over each line of output.

The amount of processing I'm doing on this text isn't a ton but it doesn't lend itself well to just piping data between a few tools. It requires custom programming.

That means my program ends up with code like this:

while read -r matched_line; do
  # This is where all of my processing occurs.
  echo "${matched_line}"
done <<< "${matches}"

And in this case ${matches} are lines returned by grep. You can also loop over the output of a program too such as done < <(grep ...). On a few hundred lines of input this takes 2 full seconds to process on my machine. Even if you do nothing except echo the line, it takes that amount of time. My custom logic to do the processing isn't a lot (milliseconds).

I also tried reading it into an array with readarray -t matched_lines and then doing a for matched_line in "${matched_lines[@]}". The speed is about the same as while read.

Alternatively if I take the same matches content and use Python using code like this:

with open(filename) as file:
    for line in file:
        print(line)

This finishes in 30ms. It's around 70x faster than Bash to process each line with only 1,000 lines.

Any thoughts? I don't mind Python but I already wrote the tool in Bash.

7 Comments
2025/02/02
11:37 UTC

0

Url-encode get string with multiple arguments?

I have one string that's like
action=query&format=json&list=allpages&aplimit=max&apfilterredir=nonredirects&apprefix=Wp/akz&apcontinue=Wp/akz/Bréhéville
If I put it into the url without encoding, it breaks because it contains special characters. If I put the whole thing into --data-urlencode it encodes the &s and treats it all as one argument.
Soo, what do I do?

2 Comments
2025/02/02
09:30 UTC

3

Custom bash script dependency graph

Hi all! Some time ago I started to write a little bash script to check some kubernetes stuffs I need to check. By the time this script has become so huge with a lot of functions and variables. Sometimes I need to edit some things but I’m starting to get lost in the functions. Is there any automated way to create a graph that contains all the functions and them dependencies?

Thank you!

10 Comments
2025/02/02
22:17 UTC

1

I need your help

Hello, I am quite new on Linux and I wanted to make a bash script that has my Linux desktop environment, customisation, apps etc at once because I switch computers quite often and don't want the hassle of doing these every time I switch devices. If it's possible a yt video would be very helpful but I appreciate all the answers. Thank you!

0 Comments
2025/02/01
23:17 UTC

1

Check for any one of the background commands run by a loop exits with success

I have a loop that runs bluetooth command in the background (tries to connect to bluetooth devices with a timeout of X seconds).

If any one of those commands run by the loop exits with success (a bluetooth device usually connects within a second, so immediately), then exit the script, else do something (i.e. timeout has passed and no connections were made).

connect_trusted() {
  local device
  for device in $(bluetoothctl devices Trusted | cut -f 2 -d ' '); do
    # this command runs in background, exiting immediately with success on
    # connection or failure after timeout of 5 seconds has passed
    bluetoothctl -t 5 connect "$device" &
  done
}

# if even just 1 device was connected, exit script immediately since no more action is needed
if connect_trusted; then
    exit 0
# else, launch bluetooth menu after 5 seconds have passed (implied when bluetooth command exits with failure)
else
   do_something
fi

How to check that "any one of the bluetoothctl -t 5 connect "$device" & commands" exited with success to then exit the script, else do_something?

2 Comments
2025/02/01
18:52 UTC

0

Anyone ever try to use Tmux + friends to build a TUI app?

Anyone ever try to use Tmux as the basis for a TUI for a bash app? Perhaps combined with dialog/whiptail, fzf, bat, watch, etc. It could even include some tmux plugins.

TUI apps similar to lazygit, lazydocker and wtfutil could possibly be quickly written as a bash script inside of a tmux layout.

Possible skeleton (untested):

#!/bin/bash

# app_name - description of app_name
# usage:
#   app_name <options>

set -euo pipefail

_dispatch() {
  case "$1" in
    "_start_tui")
      shift
      _start "$@"
      ;;
    "_pane0_1")
      shift
      _loop _pane0_1
      ;;
    "_pane0_2") 
      shift
      _loop _pane0_2
      ;;
    *)
      _start_tmux "$@"
      ;;
  esac
}

_loop() {
  while sleep 5; do "$@" || true; done
}

_start_tmux() {
  # enable tmux to run inside of tmux
  unset TMUX TMUX_PANE TMUX_PLUGIN_MANAGER_PATH tmux_version
  export TMUX_SOCKET="$(mktemp -u)"
  # re-run self with $1=_layout
  exec tmux \
    -S "$TMUX_SOCKET" \
    -p ~/.config/app_name \
    -f ~/.config/app_name/tmux.conf \
    -c "'$0' _start_tui $(printf '%q ' "$@")"
}

_start_tui() {
  # TODO: unbind the prefix key, to disable the default keybinds.
  # TODO: capture ctrl-c/INT to kill tmux (not individual pane scripts)

  _layout "$@" &
  _loop _pane0_0
}

_layout() {
  # TODO: layout panes.  examples:
  tmux split-window -h -t 0.0 "$0" _pane0_1
  tmux split-window -v -t 0.1 "$0" _pane0_2
  # TODO: settings
  # TODO: app key bindings
  # TODO: process command line options
}

# definitions of panes

_pane0_0() {
  # script for window 0 pane 0

  date
}

_pane0_1() {
  # script for window 0 pane 1

  top
}

_pane0_2() {
  # TODO: script for window 0 pane 2
}

_dispatch "$@"
3 Comments
2025/01/31
23:06 UTC

1

Can't seem to get the correct pid when using this bash script I'm working on. Any help?

When using this script: https://pastecode.io/s/py42w4xn (via userscripts on unraid)

The pid in the logs is not the same as the one that's showing when i run a ps aux | grep "[s]leep 10"

It always seems to be off one. What am I doing wrong here?

The goal is to basically reset a timer every time there's a update from inotifywait and then at the end perform a command.

Thanks!

7 Comments
2025/01/31
18:40 UTC

2

Is this the right way of processing an array with elements containing white spaces?

The following function takes a list of arguments and searches for elements in the form "--key=value" and prints them in the form "--key value", so for instance "aaa --option=bbb ccc" gets converted into "aaa --option bbb ccc".

expand_keyval_args() {
    local result=()
    for arg in "$@"; do
        if [[ "$arg" == --*=* ]]; then
            key="${arg%%=*}"
            value="${arg#*=}"
            printf "%s %q " "${key}" "${value}"
        else
            printf "%q " "${arg}"
        fi
    done
}

The way I deal with values containing white spaces (or really any character that should be escaped) is by using "%q" in printf, which means I can then do the following if I want to process an array:

local args=( ... )
local out="$(expand_keyval_args "${args[@]}")"
eval "args=(${out})"

Is it the best way of doing this or is there a better way (that doesn't involve the "eval")?

EDIT: Thank you all for your comments. To answer those who suggested getopt: I have actually illustrated here a problem I have in different places of my code, not just with argument parsing, where I want to process an array by passing its content to a function, and get an array out of it, and do it correctly even if the elements of the initial array have characters like white spaces, quotes, etc. Maybe I should have asked a simpler question of array processing rather than give one example where it appears in my code.

12 Comments
2025/01/31
17:48 UTC

1

Total Newbie at Bash Scripting

I'm trying to create a script to download and datestamp YouTube videos. I can download the videos, but it comes down with the name given by it's creator. I want to append the upload date to the front of the filename. Any help is appreciated. My script so far:

read -p "Enter YouTube URL: " yt_url

echo "YouTube URL = ${yt_url}"

read -p "Enter upload date: " upload_date

echo "Upload Date = ${upload_date}"

file=$(yt-dlp --get-filename -o "%(title)s.mp4" $yt_url)

echo "File = ${file}"

yt-dlp -f mp4 "$yt_url"

2 Comments
2025/01/31
12:06 UTC

3

Can I get a variable, at all times, to keep the output of the last command?

To capture the output of a command, I do

2>&1|tee capture.log

, but this is tedious and I find myself always needing it.

Is it possible to do some magic in the background, so that the output of the last command is always captured in an environment variable?

I don't want to prefix the command with something like "capture" and I don't want to suffix it, with "2>&1";)

I just want the variable, at all times, to keep the output of the last command.

20 Comments
2025/01/31
00:32 UTC

1

jq throwing parse errors

I have the following in a file called test.txt:

[
  [
    "a", 
    "b"
  ],
  [
    "c", 
    "d"
  ]
]

I inserted it into a shell variable like this:

$ test_records=$(cat test.txt)

When I echo test_records, I get this:

$ echo $test_records
[ [ "a", "b" ], [ "c", "d" ] ]

When I iterate through, I get the following:

$ for record in $test_records; do echo $record; done
[
[
"a",
"b"
],
[
"c",
"d"
]
]

Note the opening and closing brackets which I think are related to the issue. Anyway, when I try to pipe the result of the echo to jq, I get the following:

$ for record in $test_records; do echo $record | jq '.[0]'; done
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Expected value before ',' at line 1, column 4
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Unmatched ']' at line 1, column 1
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Expected value before ',' at line 1, column 4
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Unmatched ']' at line 1, column 1
jq: parse error: Unmatched ']' at line 1, column 1

As I said, I think this is because of the opening and closing brackets. If so, why are they there? If not, what's the issue with the filter string?

Thanks, Rob

2 Comments
2025/01/30
16:48 UTC

1

Help me improve my MySQL backup script

So I've got a working backup script for backing up MySQL databases on different database servers. The script is run every hour via cron job on an Apache server and subseqently backed up via FTP to a local NAS. I know it's not pretty, but as long as it works...

'''

#!/bin/bash

backup_dir=/backup
timestamp=$(date +%Y-%m-%dT%H:%M)
user=dbuser
backup_retention_time=10

mkdir -p "$backup_dir/$timestamp"

mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_1 | gzip -9 > ${backup_dir}/$timestamp/database_1-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver2.com' database_2 | gzip -9 > ${backup_dir}/$timestamp/database_2-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_3 | gzip -9 > ${backup_dir}/$timestamp/database_3-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver3.com' database_4 | gzip -9 > ${backup_dir}/$timestamp/database_4-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_5 | gzip -9 > ${backup_dir}/$timestamp/database_5-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_6 | gzip -9 > ${backup_dir}/$timestamp/database_6-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_7 | gzip -9 > ${backup_dir}/$timestamp/database_7-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_8 | gzip -9 > ${backup_dir}/$timestamp/database_8-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_9 | gzip -9 > ${backup_dir}/$timestamp/database_9-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_10 | gzip -9 > ${backup_dir}/$timestamp/database_10-$timestamp.sql.gz 
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_11 | gzip -9 > ${backup_dir}/$timestamp/database_11-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_12 | gzip -9 > ${backup_dir}/$timestamp/database_12-$timestamp.sql.gz 
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_13 | gzip -9 > ${backup_dir}/$timestamp/database_13-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_14 | gzip -9 > ${backup_dir}/$timestamp/database_14-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_15 | gzip -9 > ${backup_dir}/$timestamp/database_15-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver4.com' database_16 | gzip -9 > ${backup_dir}/$timestamp/database_16-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_17 | gzip -9 > ${backup_dir}/$timestamp/database_17-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_18 | gzip -9 > ${backup_dir}/$timestamp/database_18-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_19 | gzip -9 > ${backup_dir}/$timestamp/database_19-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver2.com' database_20 | gzip -9 > ${backup_dir}/$timestamp/database_20-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_21 | gzip -9 > ${backup_dir}/$timestamp/database_21-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_22 | gzip -9 > ${backup_dir}/$timestamp/database_22-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_23 | gzip -9 > ${backup_dir}/$timestamp/database_23-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_24 | gzip -9 > ${backup_dir}/$timestamp/database_24-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_25 | gzip -9 > ${backup_dir}/$timestamp/database_25-$timestamp.sql.gz

find $backup_dir -depth -type d -mtime +$backup_retention_time -exec rm -r {} \;

'''

My main goal is to implement a rolling backup/retention strategy, i.e. I want to keep

  • 24 hourly backups
  • 7 daily backups
  • 12 monthly backups

Any help is greatly appreciated!

EDIT: changed the timestamp from %Y-%m-%dT%H:%M to %Y-%m-%dT%H-%M for better compatibility.

0 Comments
2025/01/29
13:47 UTC

1

Does anyone have any good recommendations for learning awk? I've tried a few books and YT videos but I'm finding my eyes glazing over. I worked with LLMs to make it do some advanced editing and I'm super excited to learn how to actually drive it myself, but I haven't found anything that gels with me

7 Comments
2025/01/29
20:48 UTC

28

Why is "set -e" not the default or not left in scripts after creation?

"set -e" as most of you know will exit a script on error. It (and other set commands) is often used in a script during development when errors are expected and you want immediate halt.

But why is this behavior not the default anyway? Surely in vast majority of cases when a script is in production and there is an error, you would want the script to halt rather than attempt to execute the rest of it (much of which will have dependency and less likely to just be an independent process)?

25 Comments
2025/01/29
15:50 UTC

1

Get stderr and stdout separated?

How would I populate e with the stderr stream?

r="0"; e=""; m="$(eval "$logic")" || r="1" && returnCode="1"

I need to "return" it with the function, hence I cannot use a function substitution forward of 2> >()

I just want to avoid writing to a temp file for this.

9 Comments
2025/01/29
04:32 UTC

2

How to configure terminal to show the *entire* previous command, without truncating it with an ellipsis?

Using iterm2 on MacOS with ZSH and powerlevel10k and Oh-My-Zsh. Nothing unusual.

When I paste a long `curl` command (with a request body that has a few dozen lines or more) into the terminal and execute it, I want to see the entire command when I press the Up arrow key to reload the last command from my history.

But what actually happens is only the last 30 or so lines of the command are shown when I press the Up arrow key, truncating all the lines above with an ellipsis (...).

I want to configure my terminal to actually display the *whole* entire command when I press Up.

I assume this is a config issue somewhere either in my `~/.zshrc` file or the `~/.p10k.zsh` file, but have no clue if that's correct.

1 Comment
2025/01/28
16:30 UTC

8

want to print only the real time

time ./prog

real    0m0.004s
user    0m0.001s
sys     0m0.003s

but i only want to print the first line

real 0m0.004s or 0m0.004s

is there any way ?```

17 Comments
2025/01/27
12:03 UTC

4

YAML manipulating with basic tools, without yq

The problem. I have a YAML file with this:

network:
  version: 2
  renderer: networkd
  ethernets:
  wifis:
    wlx44334c47dec3:
      dhcp4: true
      dhcp6: true

As you can see, there is an empty section ethernets, but we could also have wifis section empty. This is invalid structure and I need to remove those empty sections:

This result:

network:
  version: 2
  renderer: networkd
  wifis:
    wlx44334c47dec3:
      dhcp4: true
      dhcp6: true

can be achieved easily with:

yq -y 'del(.network.ethernets | select(length == 0)) | del(.network.wifis | select(length == 0))'

But I want to achieve the same with sed / awk / regex. Any idea how?

30 Comments
2025/01/27
05:50 UTC

16

Importance of checking IFS

I just wanted to spread a word about importance of explicitly defining and assigning values to IFS.

After years of scripting in bash in Ubuntu i never thought of non standard IFS values in other linux based operating systems.

Few minutes ago figured out why some of my scripts weren’t working properly in openwrt. IFS in openwrt contains only /n newline character vs tab space and newline.

Can be checked by looking into environment via set (printenv is not installed by default) or simply by echoing IFS and piping into cat: echo “$IFS” | cat -A

Hope this will save someone down the road from wasting hours on debugging.

My scripts weren’t working simply copied to openwrt as they were working on Ubuntu and didnt show any issues at first glance. I want to pinpoint here that i didnt write in openwrt environment or else i would have checked IFS. From now on i will make a habit to assign it right after the shebang.

Thanks.

11 Comments
2025/01/25
16:42 UTC

0

HELP!

I'm trying to make a simple OS that uses BASH and coreutils as a base.
I searched and asked to chatgpt how to compile it to a unknown os and basically everything went wrong.

btw, i'm on windows 11 with nasm,gcc, mingw, msys2 and Arch WSL.
Can someone help me?

3 Comments
2025/01/25
16:36 UTC

2

Creating a simple latex launcher

Hello!

I'm not sure I'm posting in the good subreddit, don't hesitate to redirect me!

I've a little problem I'm not able to solve, because I don't understand well enough the problem to know where to search.

I would like to create a script that manages a .tex file such as :

  • it opens a terminal and launches latex -pdf -pvc $FILE, $FILE being the argument file
  • it opens the file with kwrite

Ideally, I declare this script as an application that I can set as the default application for .tex files. This way, when I double click on the file every of these actions execute themselves.

I first tried to create a latex.sh script (yes it's executable) :

#!/bin/bash
latexmk -pdf -pvc $1 &
kwrite $1 &

Then I added a .desktop file in ~/.local/share/applications and tried to open a .tex file with this application. Without surprise it does not work, but I don't really know what exactly is the process I want to see in the system so it's difficult to improve the script...

Thanks in advance for your help!

EDIT (2025-01-29): Here is the solution I get:

/home/user/.applications/latex/latex.sh

#!/bin/bash

kwrite "$1" &

konsole -e latexmk -pdf -pvc "$1" &

/home/user/.local/share/applications/latex.desktop

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/home/user/.applications/latex/latex.sh %u
Name=Latex
Icon=/home/user/.applications/latex/icon.svg
6 Comments
2025/01/23
18:30 UTC

2

Sed replacement with a variable needs single and double quotes

Hi all, this may be a stupid question, so sorry in advance. I have just started to get into the world of bash scripting, and I decided to create an install script for my NixOS build. Within that, I want to create a new host, so I have decided to use sed to add a block of Nix code from a text file in place of a comment that I have there by default. The problem arises then that I need to evaluate bash script within it using double quotes "" as well as using the s option at the start, which from what I can see only works with single quotes ''.
From what I could find when googling this, I need to exit the single quotes with double quotes when writing the expression, then go back to singles to finish it.
https://askubuntu.com/questions/1390037/using-sed-with-a-variable-inside-double-quote

So this is what i have so far sudo sed -i 's|#Install new host hook|'"$(< /etc/nixos/scripts/helperFiles/newHostFlakeBlock.txt)"'|' /etc/nixos/flake.nix

15 Comments
2025/01/24
11:34 UTC

0

Error oh my bash theme development

Good evening everyone, I'm making another theme for Oh My Bash that has the same base as my old theme, but it's not overwriting the base properly, these are the codes

New theme


if [ -z "${NEKONIGHT_BASE_LOADED}" ]; then
  source ~/.oh-my-bash/themes/nekonight/nekonight-base.sh
  export NEKONIGHT_BASE_LOADED=true
fi


icon_start="╭─"
icon_user=" 🌙 ${_omb_prompt_bold_olive}\u${_omb_prompt_normal}"
icon_host=" at 🌙 ${_omb_prompt_bold_cyan}\h${_omb_prompt_normal}"
icon_directory=" in 🌙 ${_omb_prompt_bold_magenta}\w${_omb_prompt_normal}"
icon_end="╰─${_omb_prompt_bold_white}λ${_omb_prompt_normal}"

_omb_theme_nekonight_git_prompt_info
_omb_theme_nekonight_scm_git_status

function _omb_theme_PROMPT_COMMAND() {
  PS1="${icon_start}${icon_user}${icon_host}${icon_directory} in $(_omb_theme_nekonight_git_prompt_info)\n${icon_end} "
}

_omb_util_add_prompt_command _omb_theme_PROMPT_COMMAND

Base theme

icon_start="╭─"
icon_user=" 🐱 ${_omb_prompt_bold_olive}\u${_omb_prompt_normal}"
icon_host=" at 🐱 ${_omb_prompt_bold_cyan}\h${_omb_prompt_normal}"
icon_directory=" in 🐱 ${_omb_prompt_bold_magenta}\w${_omb_prompt_normal}"
icon_end="╰─${_omb_prompt_bold_white}λ${_omb_prompt_normal}"

function _omb_theme_nekonight_git_prompt_info() {
  local branch_name
  branch_name=$(git symbolic-ref --short HEAD 2>/dev/null)
  local git_status=""

  if [[ -n $branch_name ]]; then
    git_status="${_omb_prompt_bold_white}(🐱 $branch_name $(_omb_theme_nekonight_scm_git_status))${_omb_prompt_normal}"
  fi

  echo -n "$git_status"
}

function _omb_theme_nekonight_scm_git_status() {
  local git_status=""

  if git rev-list --count --left-right @{upstream}...HEAD 2>/dev/null | grep -Eq '^[0-9]+\s[0-9]+$'; then
    git_status+="${_omb_prompt_brown}↓${_omb_prompt_normal} "
  fi

  if [[ -n $(git diff --cached --name-status 2>/dev/null) ]]; then
    git_status+="${_omb_prompt_green}+${_omb_prompt_normal}"
  fi

  if [[ -n $(git diff --name-status 2>/dev/null) ]]; then
    git_status+="${_omb_prompt_yellow}•${_omb_prompt_normal}"
  fi

  if [[ -n $(git ls-files --others --exclude-standard 2>/dev/null) ]]; then
    git_status+="${_omb_prompt_red}⌀${_omb_prompt_normal}"
  fi

  echo -n "$git_status"
}

The prompt gets all buggy, it looks like this

\\\[\\e\[97;1m\\\](🐱 main \\\[\\e\[0;31m\\\]↓\\\[\\e\[0m\\\] \\\[\\e\[0;93m\\\]•\\\[\\e\[0m\\\]\\\[\\e\[0;91m\\\]⌀\\\[\\e\[0m\\\])\\\[\\e\[0m\\\]\\\[\\e\[0;31m\\\]↓\\\[\\e\[0m\\\] \\\[\\e\[0;93m\\\]•\\\[\\e\[0m\\\]\\\[\\e\[0m\\\]╭─ 🌙 brunociccarino at 🌙 DESKTOP-27DNBRN in 🌙 \~ in (🐱 main ↓ •⌀)

╰─λ
1 Comment
2025/01/21
23:15 UTC

0

rmdir "No such file or directory" but ls shows folder isn't empty

I am trying to clean up some old media files.

When I use ls to show the contents of my current folder, it lists one folder. When I use rmdir to remove that folder, it states:

ls: folder: No such file or directory

How do I get rid of the target directory?

28 Comments
2025/01/20
21:15 UTC

1

style enforcement tool?

Hi folks, looking for a sh and bash enforcement tool. I found bashate but seems too limited.

2 Comments
2025/01/20
19:19 UTC

17

I made a simple "UI Library" for bash called basil. And a small helper for sourcing the library and creating a ready to run file.

4 Comments
2025/01/20
15:26 UTC

Back To Top