Bash Scripting: Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
Line 13: Line 13:
* "$@" expands to all command-line parameters.
* "$@" expands to all command-line parameters.
* "\n" in a variable does not necessarily works as expected. Eg, no newline is created.
* "\n" in a variable does not necessarily works as expected. Eg, no newline is created.
* \ before a newline actually escapes the newline. Thus, you can create multi-line strings or commands just by terminating a line with the \ symbol.


== Tests ==
== Tests ==

Revision as of 14:34, 17 October 2007

String Manipulations

  • To replace all substrings by another, use the following syntax:
echo ${stringZ//abc/xyz}

This would replace all occurences of abc in stringZ by xyz. The following replaces only the first match:

echo ${stringZ/abc/xyz}

Special Symbols

  • "$@" expands to all command-line parameters.
  • "\n" in a variable does not necessarily works as expected. Eg, no newline is created.
  • \ before a newline actually escapes the newline. Thus, you can create multi-line strings or commands just by terminating a line with the \ symbol.

Tests

  • -n checks if a string is not empty, -z if it is empty.
  • [ is not a keyword but a command (a program!). It is recommended to use [[ in tests which is a keyword.

Standard Input / Output

  • "<<<" can be used to feed a string as standard input.

Command Line Utilities

  • sdiff -s will generate a formatted output of the differences between two files. Very useful.