1

Is there any way I can have a bash file and pass an argument on a second line like this

sh -c
git status;
echo "hello world"

this is really ugly

sh -c "git status; echo "hello world""

I'm passing sh to another command so I can't just do:

git status;
echo "hello world"

even a way to do something like this would be nice

echo
"hello world"

essentially I need a way of not terminating on new line

looking for a prettier way of passing to commands to xargs here https://stackoverflow.com/a/6958957/340688

Community
  • 1
  • 1
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

3 Answers3

1
sh << EOF
  git status
  echo "hello world"
EOF
Srdjan Grubor
  • 2,605
  • 15
  • 17
1

In a terminal

echo \
"hello world"

or

  sh -c \
    "git status; \
    echo "hello world""
repzero
  • 8,254
  • 2
  • 18
  • 40
  • Your second example treats `git` as the argument for `-c` and `status;`, `echo`, and `"hello world"` as additional positional arguments. – chepner Dec 12 '14 at 20:38
1

Simply quote it. Quoted strings can extend over multiple lines.

sh -c '
git status
echo "hello world"'

You can play with indentation and placement of the quotation marks to find out what most closely meets your aesthetic expectations.

# Does this look nicer?
sh -c '
  git status
  echo "hello world"
'

If you want variable expansion to take place, you'd probably want to change double and single quotes.

dir=/src/project/
sh -c "
  cd $dir
  git status
  echo 'hello world'
"

Note that this becomes messy if you want more complex quoting (involving both, single and double quotes itself) in the -c argument. Consider using heredoc syntax as suggested by Srdjan Grubor in this case. Note however that it feeds the script to the shell via standard input, not as a command line parameter.

For your second example, simply escape the newline character.

echo \
  "hello world"

Be careful not to place any other spaces (or other characters, including comments) between the \ and the end of the line.

Some people, including me, find code easier to read if those backslashes are all aligned at some fixed column.

echo                  \
  "hello"             \
  "happy"             \
  "fruit gum"         \
  "world"

Your editor might help you with this.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92