Artificial intelligent assistant

Bash function that accepts input from parameter or pipe I want to write the following bash function in a way that it can accept its input from either an argument or a pipe: b64decode() { echo "$1" | base64 --decode; echo } Desired usage: $ b64decode "QWxhZGRpbjpvcGVuIHNlc2FtZQo=" $ b64decode < file.txt $ b64decode <<< "QWxhZGRpbjpvcGVuIHNlc2FtZQo=" $ echo "QWxhZGRpbjpvcGVuIHNlc2FtZQo=" | b64decode

See Stéphane Chazelas's answer for a better solution.

* * *

You can use `/dev/stdin` to read from standard input


b64decode()
{
if (( $# == 0 )) ; then
base64 --decode < /dev/stdin
echo
else
base64 --decode <<< "$1"
echo
fi
}


* `$# == 0` checks if number of command line arguments is zero
* `base64 --decode <<< "$1"` one can also use `herestring` instead of using `echo` and piping to `base64`

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 0327af2ecebea99d58ef5202fac66992