${BASH_SOURCE[0]} equivalent in zsh?

${BASH_SOURCE[0]} equivalent in zsh is ${(%):-%N}, NOT $0(as OP said, the latter failed in .zshrc) Here % indicates prompt expansion on the value, %N indicates “The name of the script, sourced file, or shell function that zsh is currently executing, whichever was started most recently. If there is none, this is equivalent to the parameter … Read more

Can I call a function of a shell script from another shell script?

Refactor your second.sh script like this: func1 { fun=”$1″ book=”$2″ printf “func=%s,book=%s\n” “$fun” “$book” } func2 { fun2=”$1″ book2=”$2″ printf “func2=%s,book2=%s\n” “$fun2” “$book2” } And then call these functions from script first.sh like this: source ./second.sh func1 love horror func2 ball mystery OUTPUT: func=love,book=horror func2=ball,book2=mystery

xargs split at newlines not spaces

Try: printf %b ‘ac s\nbc s\ncc s\n’ | xargs -d ‘\n’ bash /tmp/test.sh You neglected to quote the \n passed to -d, which means that just n rather than \n was passed to xargs as the delimiter – the shell “ate” the \ (when the shell parses an unquoted string, \ functions as an escape … Read more

How can I delete a file only if it exists?

Pass the -f argument to rm, which will cause it to treat the situation where the named file does not exist as success, and will suppress any error message in that case: rm -f — filename.log What you literally asked for would be more like: [ -e filename.log ] && rm — filename.log but it’s … Read more