2

I have to use a bash script (I'm not familiar with bash) for submitting a HPC job. The job-submission script template sets the command and options and then combines them in the end:

input="file_name"
name="simulation1"

application=my_app
options="in=$input.h5 out=$name.h5 param1=foo"

cmd="$application $options"

this all works okay, i.e. eval $cmd executes

my_app in=$input.h5 out=$name.h5 param1=foo

until I need the character '>' in the options. In particular, I want to add the option absrad="mass>1?H:0", but if I simply set

options="in=$input.h5 out=$name.h5 param1=foo absrad=mass>1?H:0"

then bash truncates that at the '>', so that eval $cmd executes

my_app in=$input.h5 out=$name.h5 param1=foo absrad=mass

instead. How to fix that such that eval $cmd executes

my_app in=$input.h5 out=$name.h5 param1=foo absrad="mass>1?H:0"

EDIT. Note that this is similar to, but not a duplicate of this post, where a single-quoted argument is the problem. Moreover, the answers there only consider solutions using an array for the options. I would like alternatives w/o array.

9
  • doesn't bash have a standard escape character? like "\>" Commented Apr 28, 2017 at 18:42
  • or single quotes might also work Commented Apr 28, 2017 at 18:43
  • Is your command called like $cmd or "$cmd" because the latter should work. Commented Apr 28, 2017 at 18:47
  • 1
    FYI - eval is not truncating per se, but scanning the string and performing variable substitution and when it gets to the '>' it sees the redirection character and will create a file called '1?H:0'. Then it passes the line to the shell for execution. Another reason to avoid using eval which can be dangerous if the variables it is operating on can be manipulated. Commented Apr 28, 2017 at 18:53
  • 1
    By closing this question you deprive the chance for an alternative (to arrays) answer. Commented Apr 29, 2017 at 11:50

1 Answer 1

1

You could always use an array for options:

declare -a options=("in=$input.h5" "out=$name.h5" "param1=foo" "absrad=\"mass>1?H:0\"");
declare -a cmd=(my_app "${options[@]}");

Then when you want to expand cmd the following should give you the correct expansion:

"${cmd[@]}"

Additionally, this gets rid of the need to use eval, which I tend to avoid whenever possible.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.