Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • The basic structure of a command-processing script
  • Defining and evaluating bash variables
  • Grouping and evaluating values using double quotes ( " " ) or single quotes ( ' ' )
  • Functions in bash, and their local variables
  • Passing arguments to scripts and functions
  • Subtleties of quoting in bash

...

There are many examples of command-processing scripts in bioinformatics: bwa, bowtie2, samtools, bedtools to name but a very few.

The step_01.sh Script

...

  • when referencing a positional argument variable with more than one digit (e.g. ${10})
  • to separate the variable evaluation from text immediately following (e.g. "${prefix}_file.txt")
    • since underscore characters ("_") are allowed in variable names, the braces are needed so that the shell does not think the variable name is prefix_file.

...

Code Block
languagebash
myvar="some text"
echo $myvar
echo ${myvar}
echo $myvar_more_text   # no output because the variable myvar_more_text is not defined
echo ${myvar}_more_text

Functions

A bash function looks like this, with or without the function keyword.

...

When defining or evaluating environment variables there's also a difference between enclosing the value in double quotes ( "$foo" ) or single quotes ( '$foo' ) – see Quoting in the shell.

Example:

Code Block
languagebash
myvar="some text"
echo "$myvar"
echo '$myvar'

Functions

A bash function looks like this, with or without the function keyword.


Code Block
languagebash
function my_function() {
  # code goes here
}



Code Block
languagebash
my_function() {
  # code goes here
}


...

Note that while a function can have many arguments, the function definition never contains anything in its ( ) "formal argument list".

And in bash, arguments passed to both scripts and functions are not enclosed in parentheses, as is the case in most programming languages.

Example:

Code Block
languagebash
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; }
myfn foo bar baz

...

The 1st (sub-command name) argument is captured in the CMD variable. Calling shift then removes that 1st argument, so that  $@ now contains everything after the sub-command name – these . So script arguments 2, 3, etc., will be positional arguments 1, 2, etc., to whatever function is called.

...

As we extend our command processing script, we'll add clauses to the case/esac block and add a short usage description to the usage function.

Note that in bash, arguments to both scripts and functions are not enclosed in parentheses, as is the case in most programming languages.

Calling a function or a script

Functions and scripts are called without

Calling a function or a script

Functions and scripts are called without parentheses around their arguments. Instead, each argument is separated by whitespace (one or more space characters).

...

Code Block
languagebash
# Call a custom script passing 2 arguments
my_script.sh arg1 "arg2 has embedded spaces"

# Call a function passing 2 arguments
my_function arg1 'arg2 has embedded spaces'Call a function passing 2 arguments
my_function arg1 'arg2 has embedded spaces'

Example:

Code Block
languagebash
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; }
myfn foo bar baz wonk
myfn foo "bar baz" wonk
myfn "foo bar" baz wonk

Running the step_01.sh script

...

Expand
titleSolution


Code Block
languagebash
# Empty quotes create an empty argument
~/workshop/step_01.sh helloWorld '' "My name is" Anna B


Quoting subtleties

You're probably already familiar with the We;ve already touched on difference kinds of Quoting in the shell. But there is an additional subtleties when handling script arguments.

...

Code Block
languagebash
# Wihout quotes, all argument grouping by the script caller is lost
helloWorld $@

# With quotes, argument quoting by the script caller is preserved
helloWorld "$@"

This is because enclosing a variable in double-quotes "" preserves any special formatting internal to the variable's value.


Tip
titleTip

It is a good idea to double-quote bash positional argument variables in order to preserve the caller's quoting.

Corollary: If some argument isn't coming through as you're expecting, it's probably a quoting issue!

...