StevieD
StevieD

Reputation: 7443

Bash config file throwing errors when sourced from Perl script

Got a line of perl that calls a bash wrapper function after it sources .bash_profile

system ('source ~/.bash_profile; osascript -e \'quit app "Chromium"\'');

Although the wrapper function gets called and is executed perfectly fine, I'm getting an error thrown from an unrelated bash function:

/Users/me/.bashrc: line 9: syntax error near unexpected token `<'
/Users/me/.bashrc: line 9: `  done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)'

This is the problem function in the .bashrc file:

source_dir() {
  while IFS= read -r -d $'\0' file; do
    source_file "$file"
  done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)
}

This bash function does not throw errors when sourcing it directly, only when loading it through the Perl script. I'm curious to know why.

I'm running bash version 5.0.2.

Upvotes: 2

Views: 83

Answers (1)

glenn jackman
glenn jackman

Reputation: 247042

perl's system uses /bin/sh as the shell (https://perldoc.perl.org/functions/system.html). It won't understand bash-specific syntax such as process substitutions.

You'll want to explicitly invoke bash:

system 'bash', '-c', q{source ~/.bash_profile; osascript -e 'quit app "Chromium"'};

Using the q{} single-quoting mechanism to avoid backslashing.


A bash note: if you invoke it as an interactive shell, it slurps in the bashrc automatically, so you should be able to do:

system 'bash', '-ic', q{osascript -e 'quit app "Chromium"'};

ref: https://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

Upvotes: 6

Related Questions