Contents / Previous / Next


Bash Script Example

Here is an example for a bash shell script that lists
all HTML files "*.html" in a directory and writes the
first line of each HTML-file to a file called File_Heads: #!/bin/sh # This is a comment echo "List of files:" ls -lA FILE_LIST="`ls *.html`" echo FILE_LIST: ${FILE_LIST} RESULT="" for file in ${FILE_LIST} do FIRST_LINE=`head -2 ${file}` RESULT=${RESULT}${FIRST_LINE} done echo ${RESULT} | cat >FILE_HEADS echo "'$RESULT' written Script done. " Each line is a distinct command
--the commands are newline-separated.
You can also separate them with a semicolons.

Variables have no type.
An ordinary variable can be expanded
with $VARNAME or ${VARNAME}.

Single forward quotes ' protect the enclosed text from
shell interpretation. $() are an alternative notation.

Double quotes "" allow all shell interpretations inside them.
They are used to group text containing whitespace.

When a command is inside backward quotes `
its output is substituted in place of the back-quotes.