Comments (11)
This is a great find and a great argument for hard tabs. Thank you.
from bashstyle.
Wow. Now I have to undo my "turn tabs into 2 spaces" everywhere.
Although, I'm not unhappy with the spaced way either. It is nested in a way that makes it stand out as unusual, in that I most often use heredocs to generate a bash script dynamically (usually executed remotely), so we might have (using 2-space indents):
#!/usr/bin/env bash
main() {
cat > "dynamic.sh" <<-EOF
#!/usr/bin/env bash
main() {
echo "Our own indentation level is distinct from the generator."
echo "Also, watch out for the inception level of interpolation."
echo "Like '$1' is the generator's first arg, while '\$1' is the generated script's first arg."
}
[[ "$0" == "${BASH_SOURCE}" ]] && main "$@"
EOF
chmod +x dynamic.sh
./dynamic.sh
}
[[ "$0" == "${BASH_SOURCE}" ]] && main "$@"
from bashstyle.
Good point, I'm "not unhappy" with this either, which I do regularly for nested scripts where you want to keep indenting. Though since Bash doesn't care about indenting it only matters if humans are expected to read generated scripts... which they aren't, they usually read the source they were generated from.
Plus you can still do this when you need while using tabs elsewhere. I don't know. Would love to hear more thoughts.
from bashstyle.
I added a variable to showcase why this had to be embedded otherwise it didn't make much sense being a fully-static text.
from bashstyle.
@delitescere You had to use <<-"EOF"
otherwise variables will be expanded.
from bashstyle.
@nikolay I think the point got across even without the variable. :)
I also didn't know about that optional variable expansion with heredocs... so you just double quote the boundary string? Can you give a full example?
from bashstyle.
cat <<-"EOF"
$No $Expansion
EOF
from bashstyle.
You can also use <<-'EOF'
. Of course, all these work without the -
as well, but leading tabs are not ignored.
from bashstyle.
Awesome, never knew about the quoted boundary string either. 😲
from bashstyle.
@progrium totally right, those generated scripts don't need the indentation retained.
from bashstyle.
This is an excellent argument for a beneficial use of tabs, and an equally good argument for having your editor's "show whitespace" menu option on speed-dial (:set list
if you use Vim).
As for advocating a mix of tabs and spaces, there is a point of diminishing returns, especially if you also spaces for visual alignment of operators and pipelines.
if [[ $doupdate
&& some-really-long-condition-that-doesnt-fit-on-one-line
&& some-other-condition-thats-also-pretty-long ]]; then
time \
updatedb --require-visibility 0 \
--add-prunenames .git \
--database-root "$BASEPATH" \
--output "$DB"
fi
Granted, that's probably a fairly unusual coding style and the text editor I use makes it trivial to maintain such code (whereas other editors might not). But Bash script doesn't have to be ugly, or hard to read. Unless that's your
plan for ensuring you have job security. ;)
The spaces-vs-tabs thing has to be a choice you make for yourself, or agree upon with your team. It will always only ever be that.
from bashstyle.
Related Issues (20)
- Why to always use "if [[ ... ]]; then; fi" instead of a conditional statement HOT 7
- Use curly braces for all variable references HOT 6
- Use [[ -n "$whatever" ]] rather than [[ "$whatever" ]] HOT 2
- Use `command -v` instead of `which` &c HOT 5
- Locale independence HOT 1
- Throwing `set -a` and `set +a` around a variable pair list HOT 2
- Using defaults and "${VAR:?}" HOT 3
- Ownership and modes HOT 1
- Tracing and Debugging HOT 2
- Mention shellcheck HOT 3
- support for differences when considering zsh/sh compatibility? HOT 2
- Guidance on documenting functions HOT 7
- set -u HOT 1
- Example of "This can be queried/extracted with a simple function using reflection." HOT 2
- Google's Bash styleguide link is broken HOT 2
- [discuss] Suffix all bash scripts with .sh
- Article reference link appears broken.
- Add license information
- Coverage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bashstyle.