0

I created a bash function to execute my C files, but it's not showing segmentation fault error when executed.

Bash function:

makex () {
    make $1 && "./$1"            
}

Output:

cc     example.c   -o example

Separating make and the command to execute the program results in the same output.

Creating a separate bash script in my bin folder results in the desired output:

cc     example.c   -o example
/home/user/bin/makex: line 28: 18981 Segmentation fault      ./$1

But I would like to keep this inside a function in a file that is in my PATH.

If there is a way to show the output exactly how it's showed when I directly execute it from the shell it would be perfect, because that is what I intended with this function:

$ make example && ./example
cc     example.c   -o example
[1]    19043 segmentation fault  ./example
Emanoel
  • 3
  • 4
  • Are you *expecting* a segmentation fault? Perhaps the build itself failed; `&&` will not execute the result if `make $1` does not succeed. – chepner Mar 01 '22 at 18:11
  • I created a bash script with the same code on my bin folder and executing it showed the segmentation fault error, so I think this is something to do with how functions work in bash. – Emanoel Mar 01 '22 at 18:14
  • 1
    You say the function is in a file in your PATH? How do you load it into your shell? – that other guy Mar 01 '22 at 18:32
  • I am sourcing it from my .zshrc file. Maybe I am mistaking, but I think this means it is in my PATH. – Emanoel Mar 01 '22 at 18:32
  • `.zshrc` is not normally loaded by Bash. This is a Zsh file. If your question is about Zsh, please edit and retag accordingly. – that other guy Mar 01 '22 at 18:33
  • A quick test suggests that Bash will print information about a segmentation fault in a program even if the program is run from a function (thankfully!). How *exactly* are you defining and running the function. [How to conceal a segmentation fault in a bash script](https://stackoverflow.com/q/28521938/4154375) might contain relevant information. – pjh Mar 01 '22 at 18:34
  • If this is really a Zsh question please remove the 'bash' tag. It's a booby trap. – pjh Mar 01 '22 at 18:36
  • I am sorry. I am using zsh. I am going to change my question. In fact when I execute the function from bash it showed the error. – Emanoel Mar 01 '22 at 18:39

0 Answers0