markvgti
markvgti

Reputation: 4619

Expanding a variable containing a command doesn't work as expected in a Bash script

I have a variable defined as follows:

#!/bin/bash
#commonvars.sh

#Plus other definitions

NUMBERED='nl -w3 -s". " '

and then I am using it in another script:

#!/bin/bash

source commonvars.sh
$NUMBERED "${DL_FILE}.titles"

but the nl command doesn't work and gives a usage error.

usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]
          [-n format] [-s sep] [-v startnum] [-w width] [file]

I am sure it must be a tiny little thing, but what exactly am I doing wrong above? The reason I am doing things this way is so that I get a standardized invocation of the nl command all over my scripts.

Running on Mac OS X 10.9.4.

Upvotes: 0

Views: 39

Answers (1)

Maximiliano Padulo
Maximiliano Padulo

Reputation: 1522

Try this:

commonvars.sh:

EDITED (I pasted the wrong one before)

NUMBERED=("nl" "-w3" "-s. ")

other script:

source commonvars.sh
"${NUMBERED[@]}"  "${DL_FILE}.titles"

You can get a better understanding of whats going on if you set the trace mode before your problematic expansions:

set -x 

Upvotes: 2

Related Questions