Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh.

more script1.sh
echo "Starting Execution"
./script2.sh
echo "End of Execution"
ln_s(){
  [[ -e $2 ]] && return
  ln -s "$1" "$2"
}

My script1.sh executes another script2.sh which has the following entry

more script2.sh
ln -s folder1 folder2

How can i automatically change all ln commands to ln_s so my custom function get called instead of the default ln command ?

I tried the adding the following inside the script1.sh but none of them work and it is picking ln command instead of ln_s

alias ln=ln_s
ln=ln_s
export ln=ln_s

Every time it calls ln instead of ln_s

Ref Thread: Ignore exit status for #!/bin/bash -e

Please suggest.

A quick reference to man bash tells me

I did put this line shopt -s expand_aliases; alias ln=ln_s in script1.sh but the output shows that that it is still picking ln instead of ln_s .
Output:

ln: cannot create folder2: File exists

Can you please explain why ?

Works excellently for me:
without expand_aliases set:

./shscr: line 13: la: command not found

with expand_aliases set:

la .
+ ls -la .
total 176
drwxrw-r-- 6 user user 57344 Dez 13 14:35 .
.
.
.

Must be your setup / version?

Please consider calling a second script i.e script2.sh from the first i.e script1.sh while having the command set in script2.sh while the alias set in script1.sh in your test.

Also find the details below.

bash-4.1$ bash --version
GNU bash, version 4.1.17(1)-release (sparc-sun-solaris2.11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
bash-4.1$ uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Sorry? You NEED to set options and aliases in the script they are used in.

I have no write permissions on script2.sh ... i just have ownership of script1.sh.

Can you please suggest ?

Can you source script2 from within script1?

Yes, i did source in script1.sh as shown below.

more script1.sh
shopt -s expand_aliases; 
alias ln=ln_s 
echo "Starting Execution"
source  ~/script2.sh
~/script2.sh 
echo "End of Execution" 
ln_s()
{   
[[ -e $2 ]] && return   
ln -s "$1" "$2" 
}

But i get this error:

/opt/user1/script2.sh: line 1: ln_s: command not found

Why the error ?

What is this:

source  ~/script2.sh
~/script2.sh 

?
And, where do you define the function, and where do you call it?

In Post# 9 i have shown that the function ln_s() is define in script1.sh.

I am sourcing script2.sh inside script1.sh here -> source ~/script2.sh and then i am executing script2.sh from within script1.sh here -> ~/script2.sh with the expectation that the ln command in script2.sh should be executed as ln_s thus calling the function.

Yes, but unfortunately too late in the script.

OK, very good, the alias should be known, but not the function

Why this second execution? And, neither alias nor function will be available

No, as explained.

Let me comment on your script snippet:

shopt -s expand_aliases;        # setting the necessary shell option
alias ln=ln_s                   # defining the alias IN THE CURRENT ENVIRONMENT in which the function IS NOT KNOWN YET
echo "Starting Execution" 
source  ~/script2.sh            # sourcing (=reading & executing) the script IN THE CURRENT ENVIRONMENT; alias known, function not known
~/script2.sh                    # running the script (again!) in a NEW subshell IN A NEW ENVIRONMENT; alias not known, function not known 
echo "End of Execution" 
ln_s()                          # defining the function for LATER USE!
{   
[[ -e $2 ]] && return   
ln -s "$1" "$2" 
}
.                               # from here on, ln will correctly call the function
.
.

I strongly recommend you get your head around shells and subshells, the difference between sourcing a collection of commands and executing them in a subshell, environment inheritance, and function definition.

1 Like

Instead of an alias, you could maybe use an exported function:

ln() {   ln_s "$@" }
export -f ln