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?

5
  • 3
    You shouldn't be making a string with these contents anyhow -- argument lists should be stored in an array. Commented May 4, 2015 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. Commented May 4, 2015 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. Commented May 4, 2015 at 17:10
  • 2
    Or, you know, see if the command in question has a better way. Commented May 4, 2015 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. Commented May 4, 2015 at 17:11

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

You provided a nice way to handle multiple arguments as well as providing an answer to the original question which is using the backlash.
Can you elaborate more on what this means ${psql_args[@]}"? How do I execute it?
@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 mywiki.wooledge.org/BashFAQ/005, for more details.
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.

Comments

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

3 Comments

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.
Oh, true. Still, too many unconsidered expansions can happen here.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.