0

Is there a particular way to setup formatting or some kind of line break for readability purposes?

For example, I have a line of code that is lengthy string like so with a lot of input parameters:

PGPASSWORD=$DB_OWNER_PWD $PSQL_HOME/psql -h $HOST_NM -p $PORT -U $DBOWNER -v v1=$DBOWNER -v v2=$DUSER -f tst.sql postgres

I would like to make it more readable in the script like this:

PGPASSWORD=$DB_OWNER_PWD $PSQL_HOME/psql -h $HOST_NM 
                     -p $PORT 
                     -U $DBOWNER 
                     -v v1=$DBOWNER 
                     -v v2=$DUSER 
                     -f tst.sql postgres

How can I do that?

noober
  • 1,427
  • 3
  • 23
  • 36
  • 3
    You shouldn't be making a string with these contents anyhow -- argument lists should be stored in an array. – Charles Duffy May 04 '15 at 17:04
  • 2
    BTW -- not a safe way to deal with passwords. You're better off storing them in a file with restricted permissions; `/proc/PID/environ` isn't always effectively protected. – Charles Duffy May 04 '15 at 17:09
  • Heh. I read "lengthy string" in the question title to think you were storing all this in a string (aka a scalar variable). If it's a long command, not a long **string**, then much of my answer was off-base. Words: They have meanings. – Charles Duffy May 04 '15 at 17:10
  • 2
    Or, you know, see if the command in question has [a better way](http://www.postgresql.org/docs/current/static/libpq-pgpass.html). – kojiro May 04 '15 at 17:11
  • 1
    BTW -- all-uppercase shell variable names are bad form, except for environment variables (like `PGPASSWORD`) and builtins (like `HOME`, `USER`, `PATH`, etc). Regular shell variable names should have at least one lower-case character to avoid accidentally overwriting variables in the other categories. – Charles Duffy May 04 '15 at 17:11

3 Answers3

4

Don't use a string variable at all for storing argument lists; use an array.

psql_args=(
  env PGPASSWORD="$DB_OWNER_PWD"  # set environment variables
  "$PSQL_HOME/psql"
  -h "$HOST_NM"                   # host to connect to
  -p "$PORT"                      # port
  -U "$DBOWNER"
  -v v1="$DBOWNER"
  -v v2="$DUSER"
  -f tst.sql                      # script to run
  postgres                        # database to run it against
)

...and to expand it...

"${psql_args[@]}"

This lets you use arguments with spaces without pain and suffering. See also BashFAQ #50.


Now, if you're not storing the list in a variable at all (and you don't need inline comments), then just use trailing backslashes.

foo \
  --bar \
  --baz
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • You provided a nice way to handle multiple arguments as well as providing an answer to the original question which is using the backlash. – noober May 04 '15 at 17:35
  • Can you elaborate more on what this means `${psql_args[@]}"`? How do I execute it? – noober May 04 '15 at 21:24
  • 2
    @noober, `"${foo[@]}"` expands the array `foo`. If you use it on a command-line alone, then that's all you need to do to execute the contents of that array as a command. See BashFAQ #5, at http://mywiki.wooledge.org/BashFAQ/005, for more details. – Charles Duffy May 04 '15 at 21:50
2

Use backslash at the end of each line:

PGPASSWORD=$DB_OWNER_PWD $PSQL_HOME/psql -h $HOST_NM  \
                 -p $PORT  \
                 -U $DBOWNER  \
                 -v v1=$DBOWNER  \
                 -v v2=$DUSER  \
                 -f tst.sql postgres

Make sure you don't have whitespace (space or tab) after the backslash.

tivn
  • 1,883
  • 14
  • 14
0

If you put the assignment inside double quotes, it will "just work" as you have it:

#!/bin/sh

FOO="one
     two
     three"

echo $FOO

when run gives:

$ /tmp/var.sh 
one two three

And same basic idea if you're actually trying to run that command:

BAR=$(echo "four
       five
       six")
echo $BAR
Moldova
  • 1,641
  • 10
  • 13
  • This is outright bad advice -- it suggests approaches that fail in very painful ways if arguments contain whitespace, quotes, or glob characters intended to be passed as literals. – Charles Duffy May 04 '15 at 17:14
  • Oh, true. Still, too many unconsidered expansions can happen here. – kojiro May 04 '15 at 17:19
  • I was just addressing the basic question of how to format something across lines, but I guess I didn't consider the larger consequences of the particular use case. – Moldova May 04 '15 at 17:21