Reputation: 7086
I am trying to create a helpful bash alias/function. I can't seem to get it to properly interpret the string parameters.
alias phpr="php -r '${*}'"
alias decode="php -r 'echo base64_decode($1);'"
function phpr ()
{
php -r "${*}"
}
function decode ()
{
php -r "echo base64_decode($1);"
}
an example of how Im trying to use decode and it failing:
decode "MT0xO2JhbGFuY2VfaXNfc2hvd249MDtiZXRfYW1vdW50PTE7YmV0X3R5cGU9MTE7ZmF2b3JpdGVfdHJhY2sxPUZSRTtmYXZvcml0ZV90cmFjazI9TU1BO2Zhdm9yaXRlX3RyYWNrMz1UQU07ZmF2b3JpdGVfdHJhY2s0PU1FRDtmYXZvcml0ZV90cmFjazU9QVFVO3dhZ2VyX3N0YXR1cz0wO2N1cnJlbnRUcmFjaz1GUkU7Y3VycmVudFJhY2U9NjtyYWNlRGF0ZT0yMDExLTA0LTI4O3JhY2V1cGRhdGVkPTs%3D"
PHP Warning: base64_decode() expects at least 1 parameter, 0 given in Command line code on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
PHP 2. base64_decode() Command line code:1
I'm not sure when I'd use a function vs an alias for cases like these.
Upvotes: 1
Views: 992
Reputation: 9172
For your alias - alias doesn't take parameters. Have you tried alias phpr="php -r"
?
For the function - it could be something being parsed in your string. Double-quotes allow parsing, Single-quotes take the string verbatim. You want the $1
to be parsed, but include any whitespace, wo you want double-quotes around that. But you can do single quotes for the rest of it:
function decode ()
{
php -r 'echo base64_decode('"$1"');'
}
Note that this includes 4 single-quotes and 2 double-quotes.
EDIT Updated with Glenn's change. If php needs the parameter to be in quotes, you may need to do:
function decode ()
{
php -r 'echo base64_decode("'"$1"'");'
}
Note that this includes 4 single-quotes and 4 double-quotes.
Ah, shell scripting...
Upvotes: 3
Reputation: 272772
I can answer one part of your question straight away: Bash aliases cannot take arguments.
Upvotes: 0