I would like to embed a long command like this in a bash script:
mycommand \
--server myserver \
--filename extremely/long/file/name/that/i/would/like/to/be/able/to/break/up/if/possible \
--otherflag \
--anotherflag
with the long filename broken up.
I could do this:
# Insufficiently pretty
mycommand \
--server myserver \
--filename extremely/long/file/name/\
that/i/would/like/to/be/able/to/break/\
up/if/possible \
--otherflag \
--anotherflag \
but it breaks the flow. I would like to be able to write this:
# Doesn't work
mycommand \
--server myserver \
--filename extremely/long/file/name/\
that/i/would/like/to/be/able/to/break/\
up/if/possible \
--otherflag \
--anotherflag
but that doesn't work because it breaks up the string literal.
Is there a way to tell bash to break a string literal but ignore any leading spaces?
You can use a variable :
One can also use an array variable
I define a short strcat function at the top of my bash script and use an inline invocation to split things up. I sometimes prefer it to using a separate variable because I can define the long literal in-line with the command invocation.
I also like this approach for when I have to enter a long CSV of values as a flag parameter because I can use it to avoid typing the comma between values:
Which is equivalent to
It's a
bit of ahack, but this works:Bash concatenates string literals that are adjacent, so we take advantage of that. For example,
echo "hi" "there"
printshi there
whereasecho "hi""there"
printshithere
.It also takes advantage of the backtick operator, and the fact that a bunch of spaces evaluates to nothing.
Basically, there is nothing built into bash to do this.
A wrapper is typically more trouble than it's worth, but that said, you could try an alias or a funciton, eg.
j