1

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.

2 Answers 2

3

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...

Sign up to request clarification or add additional context in comments.

4 Comments

you probably need some double quotes for the php script: php -r 'echo base64_decode"'"$1"'");' -- if $1 is 'foo bar', then you get php -r 'echo base64_decode("foo bar");'
ah, good point @glenn. I don't use php, so I'm not familiar with its needs. I've updated my answer.
Thanks @Tim. It works perfectly. It gives me insight into how to do the phpr one too (encapsulating the ' in ")
@Brombomb, when writing shell scripts, it's important to know the differences between the three escape mechanisms, and to remember that the quotes are simply toggles - you can enter and exit quote mode as many times as you want in a single line. Full details would be a much longer post. I'm pretty sure I've seen it documented somewhere. If I find the page, I'll link it here.
0

I can answer one part of your question straight away: Bash aliases cannot take arguments.

1 Comment

So then that'd be the use case between alias and function. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.