Im trying to write a script where an optional input of -X flag can be used, or if that info is not available download all reads.

my script as follow:

# usage : bash dump_qc.sh <run id> <number of reads>

#run id variable.

#maximum reads variable

fastq-dump --split-3 -X ${X} ${SRR}

So, if I run the script

bash dump_qc.sh SRR1234 100

Will download 100 reads from the specified SRR file.

What I would like to do is to make the second argument optional, if not specified download the whole data set!


1 Answer 1


You don't need to copy your arguments into variables, you can just use $1 directly. Anyway, this should be enough:

# usage : bash dump_qc.sh <run id> <number of reads>

if [ -z $1 ]; then
  echo "ERROR: at least one argument need to be given!" >&2
  exit 1

if [ ! -z $2 ]; then
    x="-X '$2'"

fastq-dump --split-3 "$x" "$1"
  • $\begingroup$ Thanks terdon for your solution. Im really beginner in bash programming. Could you please be more verbose and explain your code? :) $\endgroup$
    – Skepto18
    Commented Nov 19, 2019 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.