Bash script cannot be executed after comment line

Why am I unable to execute my bash script with this #-comment? My script does not execute the passed comment line. Is this due to the use of gaps \in the previous line?

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
#                     -f Test.sql
                      -f Test2.sql
+4
source share
4 answers

You cannot do this. \appends the current line to the next, so it sees bash:

"$PSQL_HOME"/psql ... -v v1=$1 -v v2=$_load # -f Test.sql
                      -f Test2.sql

You can move the comment to the last line in this particular case:

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      -f Test2.sql
#                     -f Test.sql
+8
source

, script - . , #.

- , :

"$PSQL_HOME"/psql -h $HOST_NM     \
                      -p $PORT    \
                      -U postgres \
                      -v v1=$1    \
                      -v v2=$_load \
                      $(: -f Test.sql) \
                      -f Test2.sql

$( ... ) , . : , #, , , .

+3

. , , .

psql_options=(
     -h "$HOST_NM"
     -p $PORT
     -U postgres
     -v v1="$1"
     -v v2="$_load"
     # -f Test.sql
     -f Test2.sql
)

"$PSQL_HOME"/psql "${psql_options[@]}"
+1

. \ , , .

/home/atul/myBash> cat -vte tst.sh 
echo one\$
two\$
#three\$
four$
/home/atul/myBash> chmod +x tst.sh
/home/atul/myBash> ./tst.sh
onetwo#threefour
/home/atul/myBash> 

As a bash person
COMMENTS
... a word starting with C # calls that word and
     all other characters on this line are ignored.

Tracking Modifying tst.sh File

/home/atul/myBash> cat -vte tst.sh
echo one\$
two \$
# three\$
four$
/home/atul/myBash> ./tst.sh
onetwo
./tst.sh: line 4: four: command not found
/home/atul/myBash>

Note the space after two - it does # start a word . The string is interpreted as a comment. Word four is taken as the beginning of another team.

0
source

All Articles