Score:0

Bash: Define a function in bashrc that can be used by any scripts globally

kp flag

I like to define a function in ~/.bashrc and use it in different scripts either via export -f or source .bashrc.

The function:

nano ~/.bashrc

function test_func() {
    yt-dlp -f '299+140' --merge-output-format mp4 -cia List.txt;
}

export -f test_func

The script:

#!/bin/bash

cd /home/admn/Downloads/YT_DL;

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';

Problem-1:

After test_func, rest of the command --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s' is not working at all.



Problem-2:

Earlier when I tried to use source .bashrc in my script, I was getting these errors:

/usr/local/scripts/test.sh: line 3: .bashrc: No such file or directory
/usr/local/scripts/test.sh: line 12: test_func: command not found

The function (without export -f):

nano ~/.bashrc

function test_func() {
    yt-dlp -f '299+140' --merge-output-format mp4 -cia List.txt;
}

The script (with source .bashrc):

#!/bin/bash

source .bashrc

cd /home/admn/Downloads/YT_DL;

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';

Edit-1:

$ bash -xv /usr/local/scripts/test.sh
#!/bin/bash

source /home/admn/.bashrc
+ source /home/admn/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# alias pip='pip3.7'
alias python='python3'
++ alias python=python3

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
++ case $- in
++ return

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';
+ test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s'
/usr/local/scripts/test.sh: line 5: test_func: command not found
$ 

These are some of the threads I have gone through; and though I've got some ideas, I still couldn't work out a solution for my use case. Thanks.

https://unix.stackexchange.com/questions/63665/how-to-define-a-bash-function-that-can-be-used-by-different-scripts

https://stackoverflow.com/questions/6218268/how-to-define-a-bash-function-for-use-in-any-script

https://stackoverflow.com/questions/17219174/variables-set-in-bashrc-and-accessing-them-in-shellscript

https://stackoverflow.com/questions/1500499/how-do-you-call-a-function-defined-in-bashrc-from-the-shell

Define a globally available bash function to be used by any script

OS: Ubuntu MATE 21.04

Bash: 5.1.4(1)-release (x86_64-pc-linux-gnu)

Score:3
tm flag

If you want the function to use parameters, you need to explicitly mention them.

function test_func() {
    yt-dlp "$@" -f '299+140' --merge-output-format mp4 -cia List.txt
    #      ~~~~
}

"$@" stands for "all the parameters", you can also use positional parameters like "$1", "$2", etc.

If you source a file from a different directory, you need to either specify a full path to it, or have the file's path in $PATH.

source ~/.bashrc
# or
PATH+=:~
source .bashrc

After the update: Your .bashrc contains a return that stops processing the .bashrc if not running in an interactive shell. Put the function declaration somewhere before the condition if you want to execute it in non-interactive shells, too.

tm flag
Try running your scripts under `set -xv` so you see what commands are being run.
tm flag
The `return` exits the `.bashrc` before it gets to the function declaration.
tm flag
Your `.bashrc` contains `return` that stops processing the `.bashrc` if not running in an interactive shell. Put the function declaration somewhere before the condition if you want to execute it in non-interactive shells, too.
Jags avatar
kp flag
posting function before `return` condition solved it. So, does this part `# If not running interactively, don't do anything | case $- in | *i*) ;; | *) return;; | esac` needs to go at the end of `.bashrc` file? Thank you so much.
Jags avatar
kp flag
Lastly, is there a "preferred / standard / better" way to call function from `.bashrc`? I mean, `export -f` vs `source ~/.bashrc` Thanks
tm flag
The "return" part should go before the stuff you want in an interactive shell, but don't want in a non-interactive one.
tm flag
I'm no expert but I prefer `source` to `export`.
Jags avatar
kp flag
That's what I read in few places too that `source` is preferred/better way. Thank you so much :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.