Bash Tips and Pitfalls: Difference between revisions

From miki
Jump to navigation Jump to search
m (Bash Tips moved to Bash Tips and Pitfalls: We'll also add common pitfalls to this page)
(Added pitfalls)
Line 1: Line 1:
Empty a file named <tt>filename</tt>, keeping the same permission and user/group:
Empty a file named <tt>filename</tt>, keeping the same permission and user/group:

== Tips ==


<source lang="bash">
<source lang="bash">
Line 12: Line 14:
...on 2 lines...
...on 2 lines...
</source>
</source>

== Pits ==

{| class="wikitable"
|-
!Description
!Example
|-
|'''Space!''' - Don't forget to add spaces whenever necessary, in particular around brace in '''function''' definition, or in test conditions for '''if'''s.
|
<tt>if <font color="red">''-space-''</font> [ <font color="red">''-space-''</font> -f /etc/foo <font color="red">''-space-''</font> ]; then ...</tt><br/>
<tt>function myfunc() { <font color="red">''-space-''</font> echo Hello, World!; }</tt>
|-
|'''Quote''' - Always quote parameters, variables passed to test in if ... then ... else:
|
<tt>if [ <font color="red">"$name"</font> -eq 5 ]; then ...</tt>
|-
|'''For loops with file''' - Use simply '''*''' to list files in for loops, not <tt>`ls *`</tt>:
|
<source lang="bash" enclose="valid">
for file in *; cat "$file"; done # SUCCEEDS, even if white space
for file in `ls *`; cat "$file"; done # FAILS miserably
</source>
|-
|'''Space variable setting''' - There must be no space between the variable name and the subsequent equal sign. Also the variable name must not be prefixed with a <tt>$</tt>
|
<source lang="bash" enclose="valid">
srcDir = $1 # WRONG - spaces around = sign
$srcDir=$1 # WRONG - variable name must not have $ prefix
srcDir=$1 # CORRECT
srcDir="$1" # BEST
</source>
|}

Revision as of 16:37, 22 January 2009

Empty a file named filename, keeping the same permission and user/group:

Tips

>filename

Print multi-lines text with echo:

$ echo -e "Some text\n...on 2 lines..."                    # Enable interpretation of backslash escapes (must be quoted!)
Some text
...on 2 lines...

Pits

Description Example
Space! - Don't forget to add spaces whenever necessary, in particular around brace in function definition, or in test conditions for ifs.

if -space- [ -space- -f /etc/foo -space- ]; then ...
function myfunc() { -space- echo Hello, World!; }

Quote - Always quote parameters, variables passed to test in if ... then ... else:

if [ "$name" -eq 5 ]; then ...

For loops with file - Use simply * to list files in for loops, not `ls *`:
for file in *; cat "$file"; done       # SUCCEEDS, even if white space
for file in `ls *`; cat "$file"; done  # FAILS miserably
Space variable setting - There must be no space between the variable name and the subsequent equal sign. Also the variable name must not be prefixed with a $
srcDir = $1        # WRONG - spaces around = sign
$srcDir=$1         # WRONG - variable name must not have $ prefix
srcDir=$1          # CORRECT
srcDir="$1"        # BEST