Help understanding ambiguous redirection behavior in bash
I'm working on building my own small shell that mimics bash behavior, and I'm trying to understand when and why "ambiguous redirect" errors happen.
Consider this situation:
Now these two examples behave differently:
I'm confused — why does using
Also, I noticed that in some cases,
But in this case, it doesn’t seem to:
Can someone explain when
Thanks in advance!
https://redd.it/1lf6ccw
@r_bash
I'm working on building my own small shell that mimics bash behavior, and I'm trying to understand when and why "ambiguous redirect" errors happen.
Consider this situation:
export a=" " // just a bunch of spacesNow these two examples behave differently:
ok$a"hhhhh"$.... // this is NOT ambiguous -works fineok$a"hhhhh"$USER // this IS ambiguousI'm confused — why does using
$a (which is just spaces) before a variable like $USER lead to an ambiguous redirect, but using it before a string of characters like ... doesn’t?Also, I noticed that in some cases,
$a splits the word:ok$a"hhh"$USER # gets split due to spaces in $aBut in this case, it doesn’t seem to:
ok hhhhh$... # stays as one word?Can someone explain when
$a (or any variable with spaces) causes splitting, and how this leads to ambiguous redirection errors?Thanks in advance!
https://redd.it/1lf6ccw
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Terminal Commands That I Use to Boost Programming Speed
https://medium.com/gitconnected/terminal-commands-that-i-use-to-boost-programming-speed-e76b6ef07cb0?sk=84dc6150ea662198080fe12a1f4a0b81
https://redd.it/1lfx2m3
@r_bash
https://medium.com/gitconnected/terminal-commands-that-i-use-to-boost-programming-speed-e76b6ef07cb0?sk=84dc6150ea662198080fe12a1f4a0b81
https://redd.it/1lfx2m3
@r_bash
Medium
Terminal Commands That I Use to Boost Programming Speed
Double your programming productivity with these Linux/Unix/Bash commands
smart-pause-resume: An automation noscript designed for managing multiple media players on Linux efficiently.
https://github.com/erenseymen/smart-pause-resume
https://redd.it/1lg5dbw
@r_bash
https://github.com/erenseymen/smart-pause-resume
https://redd.it/1lg5dbw
@r_bash
GitHub
GitHub - erenseymen/smart-pause-resume
Contribute to erenseymen/smart-pause-resume development by creating an account on GitHub.
how to make my bash code stop if the package manager is not valid?
https://preview.redd.it/nplcu96sng8f1.png?width=1920&format=png&auto=webp&s=00339818a850fa1992587e82186ca7269a4bf324
https://redd.it/1lhlbc2
@r_bash
https://preview.redd.it/nplcu96sng8f1.png?width=1920&format=png&auto=webp&s=00339818a850fa1992587e82186ca7269a4bf324
https://redd.it/1lhlbc2
@r_bash
Getting parent dir of file without path in one step in pure bash?
Is there an easy way to get the parent dir of a file without the path in pure bash? Or, in other words, get the substring of a variable between the last and next-to-last slash?
I know of
path='/path/to/pardir/file'
dirpath="${path%/}"
pardir="${dirpath##/}"
echo "$pardir"
pardir
With awk:
$ awk -F '/' '{sub(/\.^.+$/, "", $NF); print $(NF-1)}' <<< "$s"
$ pardir
and there's also
Is there an easy, one-step incantation with pure bash so I can get this substring between the two last slashes?
https://redd.it/1li8ylt
@r_bash
Is there an easy way to get the parent dir of a file without the path in pure bash? Or, in other words, get the substring of a variable between the last and next-to-last slash?
I know of
path='/path/to/pardir/file'
dirpath="${path%/}"
pardir="${dirpath##/}"
echo "$pardir"
pardir
With awk:
$ awk -F '/' '{sub(/\.^.+$/, "", $NF); print $(NF-1)}' <<< "$s"
$ pardir
and there's also
expr match, although I'm not good with regexes. Not to mention dirname and basename.Is there an easy, one-step incantation with pure bash so I can get this substring between the two last slashes?
https://redd.it/1li8ylt
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Update to Bash Strict Mode README
My README guettli/bash-strict-mode: Bash Strict Mode got updated.
Feedback is welcome: Please tell me, if you think something could get improved.
https://redd.it/1ljxt7e
@r_bash
My README guettli/bash-strict-mode: Bash Strict Mode got updated.
Feedback is welcome: Please tell me, if you think something could get improved.
https://redd.it/1ljxt7e
@r_bash
GitHub
GitHub - guettli/bash-strict-mode: Bash Strict Mode
Bash Strict Mode. Contribute to guettli/bash-strict-mode development by creating an account on GitHub.
bash2json - fully bash-written JSON parser
so, firstly it was created as a simple parser function for my another project, but i kinda wanted to make full JSON support in vanilla bash including arrays support, so it's fully written using bash substitution and builtins
i'd be happy to listen to any critics/suggestions
https://github.com/Tirito6626/bash2json
https://redd.it/1lkhk4i
@r_bash
so, firstly it was created as a simple parser function for my another project, but i kinda wanted to make full JSON support in vanilla bash including arrays support, so it's fully written using bash substitution and builtins
i'd be happy to listen to any critics/suggestions
https://github.com/Tirito6626/bash2json
https://redd.it/1lkhk4i
@r_bash
GitHub
GitHub - Tirito6626/bash2json: The most bash-based JSON parser
The most bash-based JSON parser. Contribute to Tirito6626/bash2json development by creating an account on GitHub.
fz - Pipe commands to FZF
Hello folks,
Last week, I was showing one of my functions sourced in
Before I share the repo with other colleagues, can you please review my code and give some comments? As a non-native English speaker, I will also appreciate if you double-check the documentation.
The purpose of the noscript is facilitating the usage of FZF with some common commands:
Last, but not least, pull requests are welcome!
Thanks a lot! Hope you like it!
https://redd.it/1llnqc4
@r_bash
Hello folks,
Last week, I was showing one of my functions sourced in
.bashrc and .zshrc to one of my colleagues at work. He liked, then I decided to make it a Bash noscript on a GitHub repo, so more people can use it. Thus, I present fz - Pipe commands to FZF!Before I share the repo with other colleagues, can you please review my code and give some comments? As a non-native English speaker, I will also appreciate if you double-check the documentation.
The purpose of the noscript is facilitating the usage of FZF with some common commands:
man, ssh and kill. If you have some useful functions or alias that could be added to the noscript, please, don't hesitate to share.Last, but not least, pull requests are welcome!
Thanks a lot! Hope you like it!
https://redd.it/1llnqc4
@r_bash
GitHub
GitHub - lfromanini/fz: Pipe commands to FZF
Pipe commands to FZF. Contribute to lfromanini/fz development by creating an account on GitHub.
"File Transfer over SHell filesystem"
Hi all
If you run Midnight Commander, and open the Right or Left menu,
then you will see this:
https://i.ibb.co/BKfgjr4Q/1menu.png
There is a MenuItem there called "Shell Link",
and If you click it and then press F1 for help,
it will show you this screen:
https://i.ibb.co/8nNRsTRN/2help.png
In short, it says that bash contains a Remote File System feature,
but when I go to bash's documentation, I don't see any mentioning of it..
So does bash really has this feature?
Thank you
https://redd.it/1lmrtcu
@r_bash
Hi all
If you run Midnight Commander, and open the Right or Left menu,
then you will see this:
https://i.ibb.co/BKfgjr4Q/1menu.png
There is a MenuItem there called "Shell Link",
and If you click it and then press F1 for help,
it will show you this screen:
https://i.ibb.co/8nNRsTRN/2help.png
In short, it says that bash contains a Remote File System feature,
but when I go to bash's documentation, I don't see any mentioning of it..
So does bash really has this feature?
Thank you
https://redd.it/1lmrtcu
@r_bash
bash2json v3 with speed tests
i just finished pretty stable bash2json v3 with huge perfomance improvents, thanks to everyone who suggested any of them
here are speed comparisons of v3, v2.3.0 and jq 1.6:
https://docs.tirito.de/bash2json/reference/results/
separate functions like query now can take as low as 3ms to finish, json validation and trim are around 1-2ms. removing forking gave a huge perfomance boost
https://redd.it/1lmx9ul
@r_bash
i just finished pretty stable bash2json v3 with huge perfomance improvents, thanks to everyone who suggested any of them
here are speed comparisons of v3, v2.3.0 and jq 1.6:
https://docs.tirito.de/bash2json/reference/results/
separate functions like query now can take as low as 3ms to finish, json validation and trim are around 1-2ms. removing forking gave a huge perfomance boost
https://redd.it/1lmx9ul
@r_bash
Ubuntu bash noscript to search for files containing certain words.
Hi all, I’m working on a Bash noscript that needs to:
Search the whole Linux filesystem (starting from /) for any files containing the text “secret” or “confidential”
For each word, count:
- How many files contain it
- The total number of occurrences across all files
- List the file names
I’m using grep -ril with --exclude-dir to avoid system folders like /proc, /sys, etc. The raw grep command works, but my loop and counting part don’t seem to produce output correctly. Any advice on how to structure this safely and correctly?
Ps:
Yes this is homework related but I've already spent a lot of time struggling so finally asking here.
The professor seems to think that throwing a unsolvable problem is some metric of "being tough". I'm totally lost as to what's the use case for such a noscript. This is what colleges are teaching these days under the banner of "cybersecurity" jfc.
https://redd.it/1lnnj5d
@r_bash
Hi all, I’m working on a Bash noscript that needs to:
Search the whole Linux filesystem (starting from /) for any files containing the text “secret” or “confidential”
For each word, count:
- How many files contain it
- The total number of occurrences across all files
- List the file names
I’m using grep -ril with --exclude-dir to avoid system folders like /proc, /sys, etc. The raw grep command works, but my loop and counting part don’t seem to produce output correctly. Any advice on how to structure this safely and correctly?
Ps:
Yes this is homework related but I've already spent a lot of time struggling so finally asking here.
The professor seems to think that throwing a unsolvable problem is some metric of "being tough". I'm totally lost as to what's the use case for such a noscript. This is what colleges are teaching these days under the banner of "cybersecurity" jfc.
https://redd.it/1lnnj5d
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Reading directories and printing file names
Hey all
Looking to make a noscript that can read all directory names in a given path and then print the directory followed by all the files in that directory matching 'log' or 'conf' in that directory.
Something like:
Yes this is for homework and ive spent hours on it, ai does it but i want to see how others approach it!
https://redd.it/1ln1tuc
@r_bash
Hey all
Looking to make a noscript that can read all directory names in a given path and then print the directory followed by all the files in that directory matching 'log' or 'conf' in that directory.
Something like:
This is a directory: /bootThese are the files in this directory: config-5.16.0-kali7-amd64, This is a directory: /boot/firmwareThese are the files in this directory: config.txt, This is a directory: /boot/grub/i386-pcThese are the files in this directory: configfile.mod, This is a directory: /devThese are the files in this directory: log, This is a directory: /etcThese are the files in this directory: adduser.conf, This is a directory: /etcThese are the files in this directory: adduser.conf.dpkg-save, This is a directory: /etcThese are the files in this directory: ca-certificates.conf, This is a directory: /etcThese are the files in this directory: dconf, This is a directory: /etcThese are the files in this directory: debconf.conf, This is a directory: /etcThese are the files in this directory: deluser.conf, This is a directory: /etcThese are the files in this directory: dns2tcpd.conf, Yes this is for homework and ive spent hours on it, ai does it but i want to see how others approach it!
https://redd.it/1ln1tuc
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Do you use process substitution in your Bash noscripts?
Discovered
https://redd.it/1low2m3
@r_bash
Discovered
<(cmd) recently, super handy. How do you use it in your noscripts and anyone else using it regularly?https://redd.it/1low2m3
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Tool Release Smart-Shell: AI-Powered Terminal Assistant with Safety, Zsh & Web Search
Hey everyone — I just released a tool I’ve been working on called Smart-Shell.
🧠 It's an AI terminal assistant that converts plain English into safe Bash/Zsh commands — and it’s not just a wrapper around an API — Well tested on Bash.
✨ Key Features:
AI-powered with Google Gemini (Pro/Flash)
Built-in 4-tier command risk analysis: ✅ Safe
🔵 Info Leak
🟡 Medium (sudo/system)
🔴 High (e.g. rm -rf)
REPL mode with smart shell detection
Supports special commands like !web, !update, !history, !creator, and more
Works with pipx, has tab completion, desktop entry, dry-run, etc.
Supports both Bash and Zsh!
📘 Docs: https://lusan-sapkota.github.io/smart-shell/
💻 GitHub: https://github.com/Lusan-sapkota/smart-shell
Happy to hear your feedback or ideas for improvement 🙌
https://redd.it/1lowr64
@r_bash
Hey everyone — I just released a tool I’ve been working on called Smart-Shell.
🧠 It's an AI terminal assistant that converts plain English into safe Bash/Zsh commands — and it’s not just a wrapper around an API — Well tested on Bash.
✨ Key Features:
AI-powered with Google Gemini (Pro/Flash)
Built-in 4-tier command risk analysis: ✅ Safe
🔵 Info Leak
🟡 Medium (sudo/system)
🔴 High (e.g. rm -rf)
REPL mode with smart shell detection
Supports special commands like !web, !update, !history, !creator, and more
Works with pipx, has tab completion, desktop entry, dry-run, etc.
Supports both Bash and Zsh!
📘 Docs: https://lusan-sapkota.github.io/smart-shell/
💻 GitHub: https://github.com/Lusan-sapkota/smart-shell
Happy to hear your feedback or ideas for improvement 🙌
https://redd.it/1lowr64
@r_bash
Generating help-options from source with sed
Here's a gist of a few snippets I've been using with larger shell noscripts and makefiles. Since source location is readily accessible in both mediums, it's just a matter of parsing to use existing comments in your help options.
Someone could probably do it more cleanly with awk, but this seemed uniquely suited to sed's concept of hold space vs pattern space. As it parses the file, sed formats each comment and pushes it to the hold space like a stack. Then, when either a target or a function declaration is matched, it prints the name along with the comments from before.
It currently relies on the function keyword in bash, so the pattern could be more generic. Otherwise, there's plenty of room to format to taste and/or add ansi colors.
https://redd.it/1lpj7tm
@r_bash
Here's a gist of a few snippets I've been using with larger shell noscripts and makefiles. Since source location is readily accessible in both mediums, it's just a matter of parsing to use existing comments in your help options.
Someone could probably do it more cleanly with awk, but this seemed uniquely suited to sed's concept of hold space vs pattern space. As it parses the file, sed formats each comment and pushes it to the hold space like a stack. Then, when either a target or a function declaration is matched, it prints the name along with the comments from before.
It currently relies on the function keyword in bash, so the pattern could be more generic. Otherwise, there's plenty of room to format to taste and/or add ansi colors.
https://redd.it/1lpj7tm
@r_bash
Gist
Self Help
Self Help. GitHub Gist: instantly share code, notes, and snippets.
Collecting Core/Thread/Storage Info from Many Linux Boxes
Got 100+ Linux machines. Need a quick inventory dump in CSV format. Just the basics per machine:
• CPU cores
• Threads per core
• Total storage
• Total memory
• Used memory
• Used storage
Not lookin’ for deep stats—just clean numbers I can toss in a dashboard.
https://redd.it/1lpwygj
@r_bash
Got 100+ Linux machines. Need a quick inventory dump in CSV format. Just the basics per machine:
• CPU cores
• Threads per core
• Total storage
• Total memory
• Used memory
• Used storage
Not lookin’ for deep stats—just clean numbers I can toss in a dashboard.
https://redd.it/1lpwygj
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community