[SOLVED] How to call a bash function from an alias
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why not call the function from the shell? What is the point of the alias?
How to do it? I thought the alias is the solution with minimal typing. I solved the task using the following trick:
functions.sh:
Code:
function hello {
echo "Hello world"
}
# the next line calls the function passed as the first parameter to the script.
# the remaining script arguments can be passed to this function.
$1 $2 $3 $4 $5
I still don't understand it but in general you need to avoid alias in such cases. (would be nice to explain how do you want to use that alias). It is not designed to work in a non-interactive environment
Quote:
Bash aliases are essentially shortcuts that can save you from having to remember long commands and eliminate a great deal of typing when you are working on the command line.
would be nice to explain how do you want to use that alias).
I have a LUKS encrypted volume, I have to run several commands to mount and unmount it. Here is the unmount function that I have created:
Code:
function closeVault() {
umount /home/usr345/texmf/tex/latex
if [ $? != 0 ]; then
exit
fi
umount /dev/mapper/vault
if [ $? != 0 ]; then
exit
fi
cryptsetup close vault
}
I want to run this function interactively from bash using a short alias like: umntvault.
you can use it as closeVault. But if you want to run it as umntvault you can rename the function or create another function which will just call this one. (or actually you can create an alias for that too:
alias umntvault=closeVault.
I have a LUKS encrypted volume, I have to run several commands to mount and unmount it. Here is the unmount function that I have created:
Code:
function closeVault() {
umount /home/usr345/texmf/tex/latex
if [ $? != 0 ]; then
exit
fi
umount /dev/mapper/vault
if [ $? != 0 ]; then
exit
fi
cryptsetup close vault
}
I want to run this function interactively from bash using a short alias like: umntvault.
Make sure the file that contains that function has been sourced and just run "closeVault". No need for an alias.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,816
Rep:
Quote:
Originally Posted by usr345
Code:
function hello {
echo "Hello world"
}
alias hello='?'
Put the function in a file, say, "myfuncts". Then source that file as shown below:
Code:
$ cat>myfuncts
function hello {
echo "Hello world"
}^D
$ source ./myfuncts
$ hello
Hello world
Note, though, that the functions in "myfuncts" may not be available "globally". Try it for yourself and see. But if you're writing scripts, you can put "myfuncts" in a directory that in your PATH ($HOME/bin is a likely place and may already be in your PATH) and source that "myfuncts" file as shown above:
Code:
$ mv ~/myfuncts $HOME/bin
$ cd /tmp
$ source myfuncts
$ hello
Hello world
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,816
Rep:
Quote:
Originally Posted by pan64
Use functions instead in your shell scripts.
This ^^^^
OP: Using aliases that are defined at login time work may work just great in the scripts that you run interactively. Eventually, though, you're going to want to run a script as a cron job and those aliases aren't going to be defined in the environment used to the run cron job. The results that are emailed to you will be full of error messages. Unless you also source the aliases definitions in the script that you are running via cron. Just for fun, try running the "hello" alias you've defined as a command under cron. You will get an error message. Using the "normal" commands is self-documenting, too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.