Bash: Define a function in bashrc that can be used by any scripts globally
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)
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.