1

I am writing notes as I prepare a system so I have something to follow if/when I need to prepare another system. Also I am hoping to use these notes and implement into a puppet configuration at some point.

I am trying to write multi-line bash commands, but when I copy and paste from my IDE (aptana) into the terminal, I get weird behavior and the commands never execute, even though when I step back in history the multi-line command looks like I entered it manually.

My question is, how can I save the commands in a multi-line format so I can quickly copy and paste into the terminal?

Example:

$ "mkdir -p /var/log/php && \
 chown -R apache /var/log/php && \ 
 chgrp -R webdev /var/log/php && \
 chmod -R 775 /var/log/php && \ 
 touch /var/log/php/oops.log"

touch /var/log/php/oops.log: No such file or directory

3 Answers 3

4

Bash control operators (such as &&) do NOT require a " \" to concatenate lines together. You also do not need to quote your script. The re-factored version would be:

mkdir -p /var/log/php && 
/bin/chown -R apache /var/log/php &&  
/bin/chgrp -R webdev /var/log/php && 
/bin/chmod -R 775 /var/log/php &&  
touch /var/log/php/oops.log

Cutting and pasting that script in a shell should work.

1
  • Interesting, I thought I tried this combo, apparently not. Your solution worked. Thanks! Dec 13, 2011 at 1:50
2

You could use exec:

$ exec <<< "echo hello world"

BUT keep in mind, it will exit your current shell once done. You could address this by spawning a new shell:

$ bash -s <<< "echo hello world"

Also, you could make it a bash script. Example:

#!/bin/bash
mkdir -p /var/log/php
chown -R apache /var/log/php
chgrp -R webdev /var/log/php
chmod -R 775 /var/log/php
touch /var/log/php/oops.log

You could do something like:

$ cat >> ~/script.sh
#!/bin/bash
mkdir -p /var/log/php
chown -R apache /var/log/php
chgrp -R webdev /var/log/php
chmod -R 775 /var/log/php
touch /var/log/php/oops.log
<CTRL+D>
$ . ~/script.sh
2
  • Was considering that... Just wanted to see if it was possible to copy and paste straight into the terminal first. Dec 13, 2011 at 1:47
  • Ah, you could address the spawning by opening a new shell. Thanks @Pascal.Charest. Dec 13, 2011 at 2:22
0

You simply do the first four commands by doing...

$ install -o apache -g webdev -m 0755 -d /var/log/php

This will create a directory /var/log/php with owner apache group webdev and permission 0755.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .