'How to let an alias propagate to another shell script
I know that it works for alias instructions in the file .bashrc. However, a shell script ln_hook:
#!/bin/sh
#filename: ln_hook
## http://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7
## https://cygwin.com/cygwin-ug-net/using-cygwinenv.html
## https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks
# export CYGWIN="winsymlinks" # ln -s: The shortcut style symlinks with file extension '.lnk'
# export CYGWIN="winsymlinks:native" # ln -s: plain text file
ln_hook(){
if [[ "-s" == "$1" ]]; then
cmd /C mklink /H "$(cygpath -aw "$3")" "`cygpath -aw "$2"`"
else
echo -e "\033[32m >> $* \033[0m"
ln "$*"
fi
}
alias ln='ln_hook'
type ln
(type ln)
# cannot propagate to another shell script
./test_ln
$(dirname $0)/test_ln
## . ./ln_hook
## type ln
and, another shell script:
#!/bin/sh
#filename: test_ln
type ln
Then run . ./ln_hook
, output:
ln_hook
ln 是 `ln_hook' 的别名
ln 是 `ln_hook' 的别名
ln 是 /usr/bin/ln
ln 是 /usr/bin/ln
Is there a workaround to make the alias effective when running other scripts?
Solution 1:[1]
After going into Bash Manual and google, export -f function_name
is what I want.
Thank Etan Reisner for the advice about function being recursive and a demo code as follow:
#!/bin/bash --posix
# Avoid an error about function xx() statement on some os, e.g. on ubuntu, Syntax error: "(" unexpected
# http://ubuntuforums.org/archive/index.php/t-499045.html
#########################################################################################################
# << A example about exporting a function >>
# hook ln and propagate it to other scripts to pollute the environment of subsequently executed commands
#########################################################################################################
## https://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7
## https://cygwin.com/cygwin-ug-net/using-cygwinenv.html
## https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks
# export CYGWIN="winsymlinks" # ln -s: The shortcut style symlinks with file extension '.lnk'
# export CYGWIN="winsymlinks:native" # ln -s: plain text file
## ln_hook
function ln(){
if [[ "-s" == "$1" ]]; then
cmd /C mklink /H "$(cygpath -aw "$3")" "`cygpath -aw "$2"`"
else
echo -e "\033[32m >>ln $* \033[0m"
command ln "$*"
fi
}
## Cannot propagate alias to other scripts
## alias ln='ln_hook'
## export -f ln=ln_hook ## ln_hook.sh: ? 23 ?:export: ln=ln_hook: ????
## http://docstore.mik.ua/orelly/unix3/upt/ch29_13.htm
## https://stackoverflow.com/questions/1885871/exporting-a-function-in-shell
export -f ln
echo&&echo "[^-^] After trying ln_hook"
echo -n "main shell: " && type ln
echo -n "subshell: " && (type ln)
echo&&echo "[^-^] Run an external script"
echo 'type ln' > test_ln.sh
./test_ln.sh
# $(dirname $0)/test_ln.sh
## . ./ln_hook
echo 'You can try: `type ln`, which will output: ln ???...'
Here, Demo or Bash shell editor and execute online
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |