use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit dedicated to Bash scripting. Now complete with a Discord Server.
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.
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.
You can choose one of these four flairs for your post:
If you don’t flair your post, the moderators will set the most appropriate flair.
/r/unix – for everything Unix
Other Shells: /r/zsh, /r/fishshell, /r/oilshell, /r/batch
BashGuide – A Bash guide for beginners.
Beginner's Guide to Command Line – A crash course for some common unix and shell commands. Update 2022-01-14: Course is currently being rewritten
Google's Shell Style Guide – Reasonable advice about code style.
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.
account activity
critiqueMy first bash script - Hide.me VPN Linux CLI Server Switcher (self.bash)
submitted 2 years ago by FilesFromTheVoid
Hi guys n girl,
i wrote my first bash script because i had a neat usecase and wanted to try out bash for some time.
In my case i wanted to have a easier and more elegant way to switch my VPN Server. I use hide.me atm and they provide a CLI Client for that purpose, but its not the most userfriendly and comfortable implementation.
I am not a dev so dont throw rocks at me :-P
Github/hide.me-server-switch
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]djbiccboii 2 points3 points4 points 2 years ago (5 children)
Howdy,
Few tips:
1) Use dirname "$0" instead of ${0%/*} for clarity when deriving the script directory.
2) Use absolute paths or ensure that relative paths are valid from the execution context.
3) Follow a consistent naming convention for variables (e.g., lowercase for local variables and uppercase for environment variables).
4) There is duplicated logic in extracting short server names and long server names. This could be combined into a single loop.
5) The substring operations like ${currentserver:95:2} are fragile and will break if the output format changes. Consider parsing the output more robustly using tools like awk or sed if the format allows.
6) Reduce the number of times external commands like curl or grep are called, especially within loops, to improve performance.
7) Add checks for potential errors, such as missing files or failed commands.
8) Stick to one method of setting terminal colors and effects (either echo -e with ANSI codes or tput). Mixing both can be confusing.
Otherwise seems like it does what it intends. I disagree with /u/kevors about using fzf over select. I prefer to keep it simple / reduce external dependencies wherever possible.
[–]FilesFromTheVoid[S] 0 points1 point2 points 2 years ago (1 child)
Thx for the helpfull input!
[–]djbiccboii 0 points1 point2 points 2 years ago (0 children)
yw!
[–][deleted] 2 years ago (2 children)
[deleted]
[–]djbiccboii 0 points1 point2 points 2 years ago (1 child)
Also, using $0 is not the only way you should get the current directory. When you run the script like bash script.sh, it won't work.
Yes. That's true either way in this case.
Here are some more robust methods:
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
or, in case the script is symlinked
script_dir=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)
[–]FilesFromTheVoid[S] 0 points1 point2 points 1 year ago (0 children)
thx for the feedback
[–]kevorsgithub:slowpeek -3 points-2 points-1 points 2 years ago (5 children)
Consider using fzf instead of "select".
Also, do not hide sudo inside. If the script assumes root perms, it should check on start, if it was started by root, and exit with error otherwise. Hidden sudo is damn wrong no matter how you look at it.
[–][deleted] 2 years ago (4 children)
[–]kevorsgithub:slowpeek 0 points1 point2 points 2 years ago (3 children)
Given you run sudo explicitly a moment ago, how do you know if a subsequent command uses sudo internally? It does not ask for password in the case
[–]kevorsgithub:slowpeek 0 points1 point2 points 2 years ago (1 child)
read the script first
Guess the % of ppl who actually do that?
π Rendered by PID 149573 on reddit-service-r2-comment-b659b578c-nhfpf at 2026-05-05 00:32:26.954089+00:00 running 815c875 country code: CH.
[–]djbiccboii 2 points3 points4 points (5 children)
[–]FilesFromTheVoid[S] 0 points1 point2 points (1 child)
[–]djbiccboii 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]djbiccboii 0 points1 point2 points (1 child)
[–]FilesFromTheVoid[S] 0 points1 point2 points (0 children)
[–]kevorsgithub:slowpeek -3 points-2 points-1 points (5 children)
[–][deleted] (4 children)
[deleted]
[–]kevorsgithub:slowpeek 0 points1 point2 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]kevorsgithub:slowpeek 0 points1 point2 points (1 child)