Search code examples
bashsymbolsbackslashquoting

BASH scripting: when to include the back slash symbol


I am writing a BASH script and I am using the bash command. Which one of the following is correct (or are both incorrect)?

bash $pbs_dir/${module_name}.${target_ID}.${instance_ID}.pbs

or

bash \$pbs_dir/\${module_name}.\${target_ID}.\${instance_ID}.pbs

Solution

  • \$ will be expanded to literal $, so there is a big difference:

    $ a="hello"
    $ echo $a
    hello
    $ echo \$a
    $a
    

    Also note that you almost always want to double quote your parameter expansions to avoid word splitting and pathname expansion:

    echo "$a"
    

    So you properly want to use the following:

    bash "$pbs_dir/${module_name}.${target_ID}.${instance_ID}.pbs"