There is this question In Bash, when to alias, when to script, and when to write a function? and it explains a lot. Your question can be addressed specifically though.
Most important thing is: an alias in Bash replaces one string with another string before the line is processed further. There is no logic; there is no separate $1
specific to the alias. There's only string replacement.
Your alias definition:
alias es='echo $SCRIPTDEST/$1'
Now if you run
es foo
then es
will be replaced by echo $SCRIPTDEST/$1
. Everything that follows stays, including the space before foo
. The result is like
echo $SCRIPTDEST/$1 foo
and it is then evaluated further. $1
here is what the shell thinks it is, it has nothing to do with the alias. From your description it looks $1
expands to an empty string. The variable is not quoted, it's another bug.
I guess you thought foo
becomes $1
in the context of the alias. If so, when you wrote "a space between $SCRIPTDEST/
and $1
" you meant what is in fact "a space between $SCRIPTDEST/$1
and foo
". This is the very space you typed between es
and foo
.
A function takes arguments and refers to them as $1
, $2
etc. The following function does what I think you wanted from your alias:
unalias es
es () {
printf '%s\n' "$SCRIPTDEST/$1"
}
Additional fixes: quoting, echo
.
$1
isn't doing what you think it is; use a function instead. This is at least partly a duplicate of "How to pass parameters to an alias?" and "Using arguments in the replacement text of an alias" – Gordon Davisson Feb 15 '20 at 04:35