LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-20-2022, 11:22 PM   #1
usr345
Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 208
Blog Entries: 1

Rep: Reputation: Disabled
How to call a bash function from an alias


Code:
function hello {
    echo "Hello world"
}

alias hello='?'
 
Old 09-21-2022, 01:29 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
You want to make an alias with the same name as the function, and for that alias to call the function? What am I missing here?
 
Old 09-21-2022, 01:39 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
I agree with evo2; you just call the fn directly, no need for an alias as well.
 
Old 09-21-2022, 01:49 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,016

Rep: Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342
using alias in scripts is not really suggested, but not impossible. See here: https://stackoverflow.com/questions/...n-shell-script
but it is still unclear what do you want to achieve
 
Old 09-21-2022, 02:01 AM   #5
usr345
Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 208

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Ok, let's call it with another name then a function:

Code:
alias helloW='?'
Quote:
Originally Posted by pan64 View Post
it is still unclear what do you want to achieve
I want to run several commands with an alias from shell. And inside a function this can be achieved easier.
 
Old 09-21-2022, 04:16 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by usr345 View Post
I want to run several commands with an alias from shell. And inside a function this can be achieved easier.
Why not call the function from the shell? What is the point of the alias?
 
Old 09-21-2022, 05:23 AM   #7
usr345
Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 208

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
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
.bashrc:
Code:
alias hello='~/functions.sh hello'
Is it possible to create a shorter solution?
 
Old 09-21-2022, 09:30 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,016

Rep: Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342
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.
Use functions instead in your shell scripts.
 
Old 09-21-2022, 10:51 AM   #9
usr345
Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 208

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 09-21-2022, 10:55 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,016

Rep: Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342Reputation: 7342
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.
 
Old 09-21-2022, 06:44 PM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by usr345 View Post
How to do it?
Minimal example. Add the following to your .bashrc (or any file that is sourced by your shell).
Code:
function hello
{
    local name=$1
    echo "hello $name I am a function"
}
Source that file, or get a new login shell, then run the command "hello usr345".

Evo2.

Last edited by evo2; 09-21-2022 at 06:45 PM.
 
1 members found this post helpful.
Old 09-21-2022, 06:46 PM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by usr345 View Post
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.

Evo2.

Last edited by evo2; 09-21-2022 at 06:47 PM.
 
1 members found this post helpful.
Old 09-21-2022, 10:21 PM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,816

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by usr345 View Post
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
HTH...
 
Old 09-21-2022, 10:30 PM   #14
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,816

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by pan64 View Post
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.
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use bash variable in bash call to php function? buddy1234567 Programming 4 09-24-2020 07:34 AM
[SOLVED] Call a function within a function in Bash Script bluethundr Linux - Newbie 3 08-23-2018 03:08 PM
Bash: how to avoid a command inside an alias being used as another alias? dedec0 Linux - Newbie 25 12-11-2017 10:20 AM
bash script to display alias commands and un-alias any less than 12 characters bani Linux - Newbie 5 01-19-2014 12:34 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration