export function from zsh to bash for use in gnu parallel

自古美人都是妖i 提交于 2019-12-08 17:17:13

问题


How do I export a function from zsh, so that I can use it in gnu parallel?

example:

function my_func(){ echo $1;}
export -f my_func
parallel "my_func {}" :::  1 2

in bash will output

1
2

whereas in zsh it will output error messages

/bin/bash: my_func: command not found
/bin/bash: my_func: command not found

回答1:


zsh does not have a concept of exporting functions. export -f somefunc will print the function definition, it will not export a function.

Instead, you can rely on the fact that bash functions are exported as regular variables starting with ():

export my_func='() { echo "$1"; }'
parallel --gnu "my_func {}" ::: 1 2 



回答2:


Based on that other guy's answer. You can write a function that export a zsh function that already defined to bash

function exportf (){
    export $(echo $1)="`whence -f $1 | sed -e "s/$1 //" `"
}

Usage

function my_func(){
    echo $1;
    echo "hello";
}

exportf my_func
parallel "my_func {}" :::  1 2



回答3:


A lot has changed since 2014.

Today you simply do:

# Activate env_parallel function (can be done in .zshenv)
. `which env_parallel.zsh`

function my_func(){ echo $1;}
env_parallel "my_func {}" :::  1 2

If your environment is big:

# Activate env_parallel function (can be done in .zshenv)
. `which env_parallel.zsh`

# Record which environment to ignore
env_parallel --session

function my_func(){ echo $1;}
env_parallel "my_func {}" :::  1 2


来源:https://stackoverflow.com/questions/22738425/export-function-from-zsh-to-bash-for-use-in-gnu-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!