First, you are better off not using any aliases as "shortcuts" inside a function definition, for reasons of readability and maintainability, and also because you are better off not encumbering yourself with aliases defined elsewhere and that may change without you remembering or even knowing that they were originally implemented with a different intent in mind in a function or script.
Second, if you must insist on using aliases inside a script / function, the bash
manpage tells you that aliases are not expanded in non-interactive shells (such as scripts). If you want them expanded in there, you need to add shopt -s expand_aliases
in the script and source
the file where those aliases are defined (generally ~/.bashrc
or ~/.bash_aliases
) for them to kick in inside the script.
For me sourcing ~/.bashrc
from inside a script is far from optimal in any situation, primarily because I don't always know what is in ~/.bashrc
on a given host's system.
If you want to be able to use calling-shell aliases from inside a script by default, you will need to either define your BASH_ENV
environment variable in your calling bash shell environment, or use #!/usr/bin/bash -i
as shebang inside the script. See man bash
and look for the section on BASH_ENV
and on how to make a script shell interactive.
But (again) this goes against good practices, meaning you or someone else will get in trouble sooner or later because of that spurious use of aliases inside a script. Do use functions instead.
So, not addressing the content of the little code you show, the way you should do this in a script called my_script
is basically:
#!/usr/bin/bash
function _pritunl() {
/Applications/Pritunl.app/Contents/Resources/pritunl-client start \
0e7e534335bsdfds6eaeb8e023a5ce4 --password XXXX"$1"
echo "attempting pritunl login"
}
function _ptki() {
_pritunl "$1"
kinit -kt ~/.keytab alexdodd@box
if [ "$DEV_STACK_NAME" -ne 1 ]; then
echo "Starting devstack"
devstack start
fi
}
_ptki "$1"
.. then make the script (my_script
) executable and run it, or just run it as:
$ bash my_script first_arg
where first_arg
corresponds to $1
, the first positional argument inside your script.
There are issues with the script above, the first one on the list being what @roaima points out in OP's comments: this may not be the right way to go about solving your problem. But if it were remember you need to:
- double-quote your variables, so they're properly expanded by the shell,
- ensure that all variables are (made) available or appropriately defined in your script (think
$DEV_STACK_NAME
). For that you probably need to pass variable(s) from the calling environment to your script. Several ways to do this are documented here and on other sites.
- use
printf
instead of echo
, and learn why it is better.
- ...