ezraspectre
ezraspectre

Reputation: 3298

how do you escape shebang line when generating a shell script

I am generating a shell script from within a script and it needs root permissions so I use

sudo bash -c "echo 'Hello There!' > '/var/www/cgi-bin/php-cgi-5.3.8'"

I get an error if I try to output the shebang line and I not sure how to escape it -- like i do with other variables.

sudo bash -c "echo '#!/bin/bash

version=\"5.3.8\"
export PHPRC=/etc/php/phpfarm/inst/php-\${version}/lib/php.ini
export PHP_FCGI_CHILDREN=3
export PHP_FCGI_MAX_REQUESTS=5000
exec /etc/php/phpfarm/inst/php-\${version}/bin/php-cgi' > '/var/www/cgi-bin/php-cgi-5.3.8'"

Where am I going wrong here?

Upvotes: 3

Views: 1518

Answers (2)

Paul
Paul

Reputation: 141827

! is looking for a command beginning with ' in your bash_history. You should have the '!' unquoted and escape it with a backslash. You may as well take the # out of quotes and escape it too, since a \ is shorter than two quotes.

sudo bash -c "echo \#\!'/bin/bash

version=\"5.3.8\"
export PHPRC=/etc/php/phpfarm/inst/php-\${version}/lib/php.ini
export PHP_FCGI_CHILDREN=3
export PHP_FCGI_MAX_REQUESTS=5000
exec /etc/php/phpfarm/inst/php-\${version}/bin/php-cgi' > '/var/www/cgi-bin/php-cgi-5.3.8'"

Upvotes: 5

gpojd
gpojd

Reputation: 23065

The ! is probably trying to replace with something in your history. Try ...'#\!/bin/bash....

Upvotes: 0

Related Questions