Sigularity Sigularity - 4 months ago 8
Linux Question

Strange behavior of shell output file ( Linux )

Do you know why below shell script just generates the output file named 'Loading_EMP.sql' instead of 'Loading_1_EMP.sql'?

#!/bin/bash

JOBID="1"
TABLE="EMP"

echo 'test' > Loading_$JOBID_$TABLE.sql;


Output



Loading_EMP.sql

Expected Output



Loading_1_EMP.sql

Answer
echo 'test'  > Loading_${JOBID}_${TABLE}.sql; 

should do it, or better

echo 'test'  > "Loading_${JOBID}_${TABLE}.sql" # to avoid word splitting

In Loading_$JOBID_$TABLE, shell trates $JOBID_ as a single variable and since it is not set, it substitutes $JOBID_ with nothing resulting in Loading_EMP.sql


This [ answer ] must read for you.