Java community use 4 spaces as the unit of indentation. 1
Ruby community use 2 spaces that is generally agreed-upon. 2
What's the standard for indentation in shell scripts? 2 or 4 spaces or 1 tab?
Java community use 4 spaces as the unit of indentation. 1
Ruby community use 2 spaces that is generally agreed-upon. 2
What's the standard for indentation in shell scripts? 2 or 4 spaces or 1 tab?
There is no standard indentation in shell scripts that matters.
Slightly less flippant answer:
I've never encountered shell specified style guide but for bash programming this is the most popular one:
Bash Style Guide and Coding Standard.pdf | lug.fh-swf.de
The indentation of program constructions has to agree with the logic nesting depth. The indentation of one step usually is in line with the tabulator steps of the editor selected. In most cases 2, 4 or 8 are chosen.
The Google Style Guide says 2 spaces
https://google.github.io/styleguide/shell.xml#Indentation
Agree with others that it is an arbitrary choice.
Its source code is hosted at: https://github.com/google/styleguide
Just open the file with vim editor and typing gg=G
will reindent the entire file. I think this is the standard.
shiftwidth
(aka sw
) vim variable. By default, it's 8 which corresponds to the default tab stop in terminals. That value is historical, few people use 8 space indentation nowadays, I've had set sw=2
in my ~/.vimrc
for decades.
– Stéphane Chazelas
Apr 15 '18 at 09:20
<<-
heredocs. https://stackoverflow.com/a/33817423/99777 You cannot do that with spaces. "For shell scripts, using tabs is not a matter of preference or style; it's how the language is defined." – joeytwiddle Mar 18 '19 at 02:28