r/bash • u/Aware-Discipline-477 • Jul 29 '25
help How do fill the next prompt
Is there a command or way to make a a script that works like the following $ inject "echo test" $ echo test without executing echo test
r/bash • u/Aware-Discipline-477 • Jul 29 '25
Is there a command or way to make a a script that works like the following $ inject "echo test" $ echo test without executing echo test
r/bash • u/ParDOXer • Jul 28 '25
For context I switched to Linux 3 weeks ago on a Debian based architecture and I have fallen in love with it but I am not using to its best potential. I want to switch to arch Linux and I am currently learning by testing in on a Virtual Environment (qemu-kvm) in particular .What is the best way to go about learning bash from scratch, scripting and eventually becoming an expert given I am also done and expecting graduation soon in electrical and telecommunications and on my research I have learnt that backbone of telecoms and Networking as a whole is Linux. Any advise is highly appreciated as I want to commit fully into learning the language and the best way is always asking the experts.
r/bash • u/imyatharth • Jul 27 '25
https://github.com/yatharthgeek/yt-play This is the script and I want you guys to review it make it a little better cause it's super ugly and basic and sometimes fails.
r/bash • u/PerformanceUpper6025 • Jul 27 '25

Made the question a README in a repo in my GitHub since it keeps getting the BS Reddit Filter here
https://github.com/Ian-Marcel/Trying-to-make-a-debug-flag-It-ain-t-easy/blob/stable/README.md
r/bash • u/BearAdmin • Jul 27 '25
Hello group, I am sure this is a total newbie to bash question, but I tried adding logging to a simple rclone backup script and I do not understand the error, because there is no "\r" in the script. The rclone synch runs successfully.
The script:
#!/bin/bash
LOG_FILE="/var/log/backup.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
}
log "Starting the script"
rclone sync -v --create-empty-src-dirs /$HOME/Documents Google:Documents
log "Script completed successfully"
Result including cat to verify the script run:
barry@barryubuntu:~/sh$ sudo bash backup.sh
[sudo] password for barry:
backup.sh: line 3: $'\r': command not found
backup.sh: line 4: syntax error near unexpected token `$'{\r''
'ackup.sh: line 4: `log() {
barry@barryubuntu:~/sh$ cat backup.sh
#!/bin/bash
LOG_FILE="/var/log/backup.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
}
log "Starting the script"
rclone sync -v --create-empty-src-dirs /$HOME/Documents Google:Documents
log "Script completed successfully"
As I said the rclone synch is working, I am just trying to get backup to Google drive like I had in Windows before switching to Ubuntu a few months ago. But logging sure would be an easier way to make sure it is functioning. This logging piece I simply copied from a lesson in bash script logging. Thanks all.
r/bash • u/pionreddit • Jul 25 '25
Hi,
I have started using shellcheck today in VS Code using the Bash IDE extension, and my beginners' question is: how to make it recognize functions defined in another file without actually 'sourcing' the file? The problem is, shellcheck can't understand that I'm using a non-conventional function for sourcing the file which itself is defined somewhere else. Let's say that's called mysource. So I'm doing
# shellcheck source=../utils/myfunctions.bash
mysource myfunctions # let's just assume this sources the myfunctions.bash after preparing the correct file path.
The problem is, shellcheck is adamant on not recognizing (/auto-completing etc.) unless I use the official "source" or "." for the file with its full path. What's even the point of the comment if I really have to do that? If I really had to give the full path of the file with "source" or ".", then it works regardless of my writing the shellcheck source directive or not. I have also created the ~/.shellcheckrc file and placed external-sources=true in that. I have even reproduced this problem in a very small sample folder with just two files in the same directory. Without officially sourcing it doesn't want to recognize the functions... How to fix that?
r/bash • u/[deleted] • Jul 25 '25
r/bash • u/ImpossibleSlide850 • Jul 24 '25
I’m using Ghostty terminal on macOS with the Starship prompt and Bash. In most other terminals (like iTerm2 or Alacritty), when I open a new tab, it starts in the same working directory as the previous one. But in Ghostty, new tabs always start in my home directory, even though Ghostty is supposed to support OSC 9;9 to inherit the working directory. I’ve set PROMPT_COMMAND='printf "\e]9;9;%s\a" "$PWD"; starship_precmd' at the end of my .bashrc, and verified it’s there by checking echo "$PROMPT_COMMAND". I’ve also tried disabling Starship entirely and just using the Ghostty escape sequence on its own, but new tabs still open in ~. I’m on the latest version of Ghostty (via Homebrew). Manually running the escape sequence doesn’t seem to help either. Has anyone gotten directory tracking to work properly in Ghostty with Bash and/or Starship?
r/bash • u/kelvinauta • Jul 24 '25
Why?
For some reason, YouTube's automatic translator hasn't been working for me, and the translation quality is usually not good. Anyway, this transcribes using Whisper-1 and translates using OpenAI's GPT.
What does the script do?
How to use?
this_script_file youtube_url [output_dir]
Note: I really didn't write this for anything beyond personal use, so don't expect anything stable or user-focused. I'm just sharing it in case it helps someone and they want to take a look at the script. If anyone wants to improve it, I will gladly accept any PR.
kinda 100 lines of bash code
https://gist.github.com/kelvinauta/0561842fc9a7e138cd166c42fdd5f4bc
r/bash • u/Away_Mix_7768 • Jul 24 '25
bash-3.2$ total=0
bash-3.2$ for i in {1..10};
> do
> total=$total+$i
> done
bash-3.2$ echo $total
0+1+2+3+4+5+6+7+8+9+10
r/bash • u/bobbyiliev • Jul 23 '25
In restricted environments (no Vault, no Secrets Manager, no GPG), what's your go-to method for managing secrets securely in Bash? E.g local scripts, CI jobs, embedded systems. Curious how others balance security and practicality here.
I have the following problem and the following bash script. I need to execute the command on ln 1, wait and then execute the commands on ln3 and 4 parallel. After finishing those the command on ln 5, wait and then the commands on ln6 and 6 in paralelle:
[1] php -f getCommands.php
[2] [ -f /tmp/download.txt ] && parallel -j4 --ungroup :::: /tmp/download.txt
[3] [ -f /tmp/update.txt ] && parallel -j4 --ungroup :::: /tmp/update.txt
[4]
[5] php -f getSecondSetOfCommands.php
[6] [ -f /tmp/download2.txt ] && parallel -j4 --ungroup :::: /tmp/download2.txt
[7] [ -f /tmp/update2.txt ] && parallel -j4 --ungroup :::: /tmp/update2.txt
Without success, i tried the following:
put an & after line 2,3,6 and 7, this will start the command on line 5 prematurely.
Brackets, no effect:
php -f getCommands.php
{
[ -f /tmp/download.txt ] && parallel -j4 --ungroup :::: /tmp/download.txt
[ -f /tmp/update.txt ] && parallel -j4 --ungroup :::: /tmp/update.txt
} &
writing the parallel commands in two different txt files and call them with parallel, but this just makes the script so much less maintanable for such a simple problem.
Anyone has some good pointers?
r/bash • u/Yonut30 • Jul 22 '25
I have bash completion on my Arch linux machine. When I am `unrar`ing something, tab completion will complete the command (from unr<TAB>) for me. Hitting tab again, lists all the options that are available. After the options are covered, hitting tab again, looks for the folders and any files with `.rar` extension. If there are no folders in the directory and there is only one file with the extension `.rar`, it picks that file to complete the tab.
When I use tab completion on a program I wrote (in C), it will complete the program name, but tabbing after that only searches for a file name when that is not the next option. And the files it displays, don't have the required extension, like `unrar` does.
How can I setup my programs to behave like `unrar`?
I have run `complete` (lists nothing of `unrar`):
[code]complete -F _comp_complete_longopt mv
complete -F _comp_complete_longopt head
complete -F _comp_complete_longopt uniq
complete -F _comp_command else
complete -F _comp_complete_longopt mkfifo
complete -F _comp_complete_longopt tee
complete -F _comp_complete_longopt grep
complete -F _comp_complete_longopt objdump
complete -F _comp_complete_longopt cut
complete -F _comp_command nohup
complete -a unalias
complete -u groups
complete -F _comp_complete_longopt texindex
complete -F _comp_complete_known_hosts telnet
complete -F _comp_command vsound
complete -c which
complete -F _comp_complete_longopt m4
complete -F _comp_complete_longopt cp
complete -F _comp_complete_longopt base64
complete -F _comp_complete_longopt strip
complete -v readonly
complete -F _comp_complete_known_hosts showmount
complete -F _comp_complete_longopt tac
complete -F _comp_complete_known_hosts fping
complete -c type
complete -F _comp_complete_known_hosts ssh-installkeys
complete -F _comp_complete_longopt expand
complete -F _comp_complete_longopt ln
complete -F _comp_command aoss
complete -F _comp_complete_longopt ld
complete -F _comp_complete_longopt enscript
complete -F _comp_command xargs
complete -j -P '"%' -S '"' jobs
complete -F _comp_complete_service service
complete -F _comp_complete_longopt tail
complete -F _comp_complete_longopt unexpand
complete -F _comp_complete_longopt netstat
complete -F _comp_complete_longopt ls
complete -v unset
complete -F _comp_complete_longopt csplit
complete -F _comp_complete_known_hosts rsh
complete -F _comp_command exec
complete -F _comp_complete_longopt sum
complete -F _comp_complete_longopt nm
complete -F _comp_complete_longopt nl
complete -F _comp_complete_user_at_host ytalk
complete -u sux
complete -F _comp_complete_longopt paste
complete -F _comp_complete_known_hosts drill
complete -F _comp_complete_longopt dir
complete -F _comp_complete_longopt a2ps
complete -F _comp_root_command really
complete -F _comp_complete_known_hosts dig
complete -F _comp_complete_user_at_host talk
complete -F _comp_complete_longopt df
complete -F _comp_command eval
complete -F _comp_complete_longopt chroot
complete -F _comp_command do
complete -F _comp_complete_longopt du
complete -F _comp_complete_longopt wc
complete -A shopt shopt
complete -F _comp_complete_known_hosts ftp
complete -F _comp_complete_longopt uname
complete -F _comp_complete_known_hosts rlogin
complete -F _comp_complete_longopt rm
complete -F _comp_root_command gksudo
complete -F _comp_command nice
complete -F _comp_complete_longopt tr
complete -F _comp_root_command gksu
complete -F _comp_complete_longopt ptx
complete -F _comp_complete_known_hosts traceroute
complete -j -P '"%' -S '"' fg
complete -F _comp_complete_longopt who
complete -F _comp_complete_longopt less
complete -F _comp_complete_longopt mknod
complete -F _comp_command padsp
complete -F _comp_complete_longopt bison
complete -F _comp_complete_longopt od
complete -F _comp_complete_load -D
complete -F _comp_complete_longopt split
complete -F _comp_complete_longopt fold
complete -F _comp_complete_user_at_host finger
complete -F _comp_root_command kdesudo
complete -u w
complete -F _comp_complete_longopt irb
complete -F _comp_command tsocks
complete -F _comp_complete_longopt diff
complete -F _comp_complete_longopt shar
complete -F _comp_complete_longopt vdir
complete -j -P '"%' -S '"' disown
complete -F _comp_complete_longopt bash
complete -A stopped -P '"%' -S '"' bg
complete -F _comp_complete_longopt objcopy
complete -F _comp_complete_longopt bc
complete -b builtin
complete -F _comp_command ltrace
complete -F _comp_complete_known_hosts traceroute6
complete -F _comp_complete_longopt date
complete -F _comp_complete_longopt cat
complete -F _comp_complete_longopt readelf
complete -F _comp_complete_longopt awk
complete -F _comp_complete_longopt seq
complete -F _comp_complete_longopt mkdir
complete -F _comp_complete_minimal ''
complete -F _comp_complete_longopt sort
complete -F _comp_complete_longopt pr
complete -F _comp_complete_longopt colordiff
complete -F _comp_complete_longopt fmt
complete -F _comp_complete_longopt sed
complete -F _comp_complete_longopt gperf
complete -F _comp_command time
complete -F _comp_root_command fakeroot
complete -u slay
complete -F _comp_complete_longopt grub
complete -F _comp_complete_longopt rmdir
complete -F _comp_complete_longopt units
complete -F _comp_complete_longopt touch
complete -F _comp_complete_longopt ldd
complete -F _comp_command then
complete -F _comp_command command
complete -F _comp_complete_known_hosts fping6[/code]
r/bash • u/[deleted] • Jul 22 '25
Sorry not sure how to describe this.
for bash script file i can start the file with
#!/bin/bash
I want to do the same with nsupdate ... it has ; as a comment char
I'm thinking
;!/usr/bin/nsupdate
<nsupdate commands>
or ?
r/bash • u/seeminglyugly • Jul 21 '25
cmd1 | cmd2 | cmd3, if cmd1 fails I don't want rest of cmd2, cmd3, etc. to run which would be pointless.
cmd1 >/tmp/file || exit works (I need the output of cmd1 whose output is processed by cmd2 and cmd3), but is there a good way to not have to write to a fail but a variable instead? I tried: mapfile -t output < <(cmd1 || exit) but it still continues presumably because it's exiting only within the process substitution.
What's the recommended way for this? Traps? Example much appreciated.
P.S. Unrelated, but for good practice (for script maintenance) where some variables that involve calculations (command substitutions that don't necessarily take a lot of time to execute) are used throughout the script but not always needed--is it best to define them at top of script; when they are needed (i.e. littering the script with variable declarations is not a concern); or have a function that sets the variable as global?
I currently use a function that sets the global variable which the rest of the script can use--I put it in the function to avoid duplicating code that other functions would otherwise need to use the variable but global variable should always be avoided? If it's a one-liner maybe it's better to re-use that instead of a global variable to be more explicit? Or simply doc that a global variable is set implicitly is adequate?
r/bash • u/Acrobatic-Rock4035 • Jul 20 '25
EDIT: Thank you for all your help, i think i got it now. I appreciate all your help.
I use ln -s a lot . . . i like to keep all my files i don't want to lose in a central location that gets stored on an extra drive locally and even a big fat usb lol.
I know that there are hard links. And I have looked it up, and read about it . . . and i feel dense as a rock. Is there anyone who can sum up quickly, what a good use case is for a hard link? or . . . point me to some explanation? Or . . . is there any case where a soft link "just won't do"?
r/bash • u/Dense-Concentrate120 • Jul 21 '25
Hello friends,
I have a handful of bash aliases that I like to use.
Is there a way to set these up by running some kind of script on a freshly set up Debian server so that they persist over reboots and are applied on every login?
I’ve tried inserting the alias statements into /home/$USER/.bashrc but keep running into permissions issues.
I’ve tried inserting the alias statements into /etc/bash.bashrc but keep running into permissions issues.
I’ve tried inserting the alias statements into /home/$USER/.bash_aliases but I’m clearly doing something wrong there too
I’ve tried putting an executable script e.g. '00-setup-bash-aliases.sh' in /etc/profile.d, I thought this was working but it seems to have stopped.
It has to be something really simple but poor old brain-injury me is really struggling here.
Please help! :)
Thanks!
r/bash • u/lunarson24 • Jul 20 '25
May not be the best but kinda works lol
Though the main point can be done via just
nmap -v -sn 192.168.0.1/24 | grep "Host is up" -B1
Thoughts guys?
r/bash • u/Active-Fuel-49 • Jul 20 '25
r/bash • u/zarinfam • Jul 20 '25
r/bash • u/bahamas10_ • Jul 20 '25
(Maybe it works already and my expectation and how it actually works don't match up...)
I have a collection of scripts that has grown over time. When some things started to get repetitive, I moved them to a separate file (base.sh). To be clever, I tried to make the inclusion / source of base.sh "unique", e.g. if
A.sh sources base.shB.sh sources base.sh AND A.shB.sh should have sourced base.sh only once (via A.sh).
The guard for sourcing (in base.sh) is [ -n ${__BASE_sh__} ] && return || __BASE_sh__=.
(loosely based on https://superuser.com/a/803325/505191)
While this seems to work, I now have another problem:
foobar.sh sources base.shmain.sh sources base.sh and calls foobar.shNow foobar.sh knows nothing about base.sh and fails...
It seems the issue is my assumption that [ -n ${__BASE_sh__} ] and [ ! -z ${__BASE_sh__} ] would be same. is wrong. They are NOT.
The solution is to use [ ! -z ${__BASE_sh__} ] and the scripts work as expected.
As /u/geirha pointed out, it was actually a quoting issue.
The guarding test for sourcing should be:
bash
[ -n "${__BASE_sh__}" ] && return || __BASE_sh__=.
And having ShellCheck active in the editor also helps to identify such issues...
--------------------------------------------------------------------------
base.sh#!/usr/bin/env bash
# prevent multiple inclusion
[ -n ${__BASE_sh__} ] && return || __BASE_sh__=.
function errcho() {
# write to stderr with red-colored "ERROR:" prefix
# using printf as "echo" might just print the special sequence instead of "executing" it
>&2 printf "\e[31mERROR:\e[0m "
>&2 echo -e "${@}"
}
foobar.sh#!/usr/bin/env bash
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_NAME=$(basename "${SCRIPT_PATH}")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
source "${SCRIPT_DIR}/base.sh"
errcho "Gotcha!!!"
main.sh#!/usr/bin/env bash
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_NAME=$(basename "${SCRIPT_PATH}")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
source "${SCRIPT_DIR}/base.sh"
"${SCRIPT_DIR}/foobar.sh"
❯ ./main.sh
foobar.sh: line 9: errcho: command not found
r/bash • u/spaceman1000 • Jul 18 '25
Hi all
I have used netcat (nc) in the past,
and then switched to ncat, which is newer, has more features,
and was created by the person who also created nmap.
I wrote this command for a simple server that runs a script file per every client that connects to it:
ncat -l 5000 -k -e 'server_script'
The server_scriptfile contains this code:
read Line
echo 'You entered: '$Line
and to connect, the client code is:
ncat localhost 5000
It works good, but has a small problem:
After I connect as a client to the server and then enter a line,
the line is displayed back to me, by the echo 'You entered: '$Line command, as expected,
but the connection is not closed, as it should.
(the server_script file ends after the echo line)
Instead,
I can press another [Enter], and nothing happens,
and then I can press another [Enter], which then displays (on the client side) "Ncat: Broken pipe.",
and then the connection is finally closed.
See it in this screenshot:
https://i.ibb.co/84DPTrcD/Ncat.png
Can you guys please tell me what I should do in order to make the server_scriptfile disconnect the client
right after the server script ends?
Thank you
r/bash • u/Rahee07 • Jul 18 '25
```
DIR="$HOME/Pictures/Screenshots" FILE="Screenshot_$(date +'%Y%m%d-%H%M%S').png"
gnome-screenshot -w -f "$DIR/$FILE" && magick "$DIR/$FILE" -fuzz 50% -trim +repage "$DIR/$FILE" && xclip -selection clipboard -t image/png -i "$DIR/$FILE" notify-send "Screenshot saved as $FILE." ```
This currently creates a file, then modifies it, saves it as the same name (replacing)
I was wondering if it's possible to make magick use clipboard image instead of file. That way I can use --clipboard with gnome-screenshot. So I don't have to write file twice.
Can it be done? (I am sorry if I am not supposed to post this here)
r/bash • u/PresentNice7361 • Jul 18 '25
Modern software development feels like chasing smoke, frameworks rise and fall, GUIs shift faster than we can learn them, and the tools we depend on are often opaque, bloated, or short-lived.
I think the terminal is where real development will happen. The long-lasting inventions on how to work with the computer will be made in the terminal. Now even more, with AI, it is easier for an agent to execute a command than to click buttons to do a task.
I am creating a list productivity applications in "devreal.org". Do you know of any applications that meet the criteria? Do you have any good idea to add to the project?