Giter Site home page Giter Site logo

Tabs are not evil! about bashstyle HOT 11 OPEN

progrium avatar progrium commented on June 15, 2024
Tabs are not evil!

from bashstyle.

Comments (11)

progrium avatar progrium commented on June 15, 2024

This is a great find and a great argument for hard tabs. Thank you.

from bashstyle.

delitescere avatar delitescere commented on June 15, 2024

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.

progrium avatar progrium commented on June 15, 2024

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.

nikolay avatar nikolay commented on June 15, 2024

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.

nikolay avatar nikolay commented on June 15, 2024

@delitescere You had to use <<-"EOF" otherwise variables will be expanded.

from bashstyle.

progrium avatar progrium commented on June 15, 2024

@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.

nikolay avatar nikolay commented on June 15, 2024
cat <<-"EOF"
    $No $Expansion
EOF

from bashstyle.

nikolay avatar nikolay commented on June 15, 2024

You can also use <<-'EOF'. Of course, all these work without the - as well, but leading tabs are not ignored.

from bashstyle.

delitescere avatar delitescere commented on June 15, 2024

Awesome, never knew about the quoted boundary string either. 😲

from bashstyle.

delitescere avatar delitescere commented on June 15, 2024

@progrium totally right, those generated scripts don't need the indentation retained.

from bashstyle.

ernstki avatar ernstki commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.