You can't use an alias to add stuff after arguments, you need a function. Use an alias only to give an alternate name to a command (alias myalias=mycommand
) or to supply initial arguments (alias myalias='foo --option1 --option2'
).
In bash, you can use any of function myfunction { … }
or myfunction () { … }
or function myfunction () { … }
to define a function. The form myfunction () …
has the advantage of being portable to all sh shells. The forms with the function
keyword have the advantage of working even if myfunction
is an alias (you can use \myfunction () …
to make the standard form work in that case). Apart from the alias thing, these syntaxes are exactly equivalent in bash.
What follows the function name or the ()
must be a well-formed complex command. Judging by your answer (the code in your question does not produce the error message, obviously you didn't post the version you tested), your mistake was that the complex command you wrote is not correct. The braces {
and }
are only recognized as begin-list and end-list syntax when they are the first thing in a command, so you need a line break or a semicolon before the closing brace.
Additionally, instead of passing two arguments to diff
and ignoring the others, you should pass them all. That's what "$@"
is for.
function dif () { diff "$1" "$2" | less; }
Furthermore, customizations for interactive bash sessions should go into .bashrc
, not .bash_profile
. .bash_profile
is only read by login shells, it isn't read when you open a new instance of bash, e.g. in a terminal. Because of a design defect in bash, .bashrc
is not read by login shells even if they're interactive, so you should put this line in your .bash_profile
:
if [[ $- == *i* ]]; then . ~/.bashrc; fi
and put interactive customization such as aliases, functions like dif
, key bindings, prompts, etc. in ~/.bashrc
.
~/.bash_profile
into shellcheck.net and fix the errors that shellcheck finds. If the meaning of the error messages is not obvious, ask here..bash_profile
file instead. Look for unterminated strings, if-statements or loops.